【问题标题】:How to run simultaneous searches in core-data?如何在核心数据中运行同时搜索?
【发布时间】:2013-03-21 16:28:23
【问题描述】:

我正在尝试了解如何在核心数据中运行同时搜索。

这是我的示例,但它不起作用,因为其中一个 GCD 似乎永远不会激活 如果我将自定义 MOC 留在其中,我会收到错误消息“无法找到实体 'Recipe' 的模型”

-(void)performSearch:(NSString*)name{

    //TODO: Is there a better way

    //In case the previous search hasn't finished
    if (globaDispatchRequestInprogress) {
        //Send on GCD
        dispatch_queue_t searchQueque = dispatch_queue_create("search queque 2", NULL);
        dispatch_async(searchQueque, ^{
            NSLog(@"\n\nDispatch 2 In Progress*******\n\n");


            //Init local variables
            NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
            NSError *error;


             //Create own MOC for multiThread
             NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc]init];

             [tempContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];


            NSPredicate *recipeName = [NSPredicate predicateWithFormat:@"ANY recipe.name ==[c] %@",name];

            //Set predicate to fetch request
            [fetchRequest setPredicate:recipeName];

            //Set query. We are searching recipes
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:tempContext];
            //sets up fetch request details
            [fetchRequest setEntity:entity];

            //Attempt to speed up program
            [fetchRequest setReturnsObjectsAsFaults:NO];


            //Perform fetch assign to return array
            NSArray*records = [tempContext executeFetchRequest:fetchRequest error:&error];

            //Add to temporary results
            //TODO: Add to NSDictionary
            [self addResultsToTemporaryResults:records];

            NSLog(@"Total results = %i",[_temporaryResultsArray count]);

            NSLog(@"\n\nDispatch 2 END**************\n\n");

        });

    }
    //Send on GCD
    dispatch_queue_t searchQueque = dispatch_queue_create("search queque", NULL);
    dispatch_async(searchQueque, ^{
        NSLog(@"\n\nDispatch In Progress*******\n\n");

        //Set flag
        globaDispatchRequestInprogress=YES;

        //Init local variables
        NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
        NSError *error;


         //Create own MOC for multiThread
         NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc]init];

         [tempContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];

        NSPredicate *recipeName = [NSPredicate predicateWithFormat:@"ANY recipe.name ==[c] %@",name];

        //Set predicate to fetch request
        [fetchRequest setPredicate:recipeName];

        //Set query. We are searching recipes
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:tempContext];
        //sets up fetch request details
        [fetchRequest setEntity:entity];

        //Attempt to speed up program
        [fetchRequest setReturnsObjectsAsFaults:NO];

        //Perform fetch assign to return array
        NSArray*records = [tempContext executeFetchRequest:fetchRequest error:&error];

        //Add to temporary results
        //TODO: Add to NSDictionary
        [self addResultsToTemporaryResults:records];

        NSLog(@"Total results = %i",[_temporaryResultsArray count]);
        globaDispatchRequestInprogress=NO;

        NSLog(@"\n\nDispatch END**************\n\n");

    });

}

【问题讨论】:

    标签: ios core-data grand-central-dispatch nsmanagedobjectcontext


    【解决方案1】:

    我看到了一些让我怀疑的事情,但没有明显的确凿证据。

    如果您看到“无法找到模型”,这表明您的持久存储协调器没有按照您认为的方式进行配置。 NSLog self.persistentStoreCoordinator.managedObjectModel 和 self.persistentStoreCoordinator.managedObjectModel.entitiesByName 会很有趣。

    Core Data 的首选 GCD 方法是使用 performBlock: 或 performBlockAndWait:,并为您的托管对象上下文使用适当的并发类型。见http://developer.apple.com/library/mac/#releasenotes/DataManagement/RN-CoreData/index.html

    您将获取的结果保存在 addResultsToTemporaryResults: 调用中。我们看不到它的来源,但它是线程安全的吗?您找到的那些记录在您获取它们的 tempContext 之外不存在,并且只能从找到它们的线程中访问。您可能希望在那里使用 NSManagedObjectID(也许您已经在使用)。

    您对 dispatch_queue_create() 的第二次调用将始终被执行。您的意思是执行 if-else 而不是简单的 if?

    当你执行 -executeFetchRequest:error: 时,检查结果。如果是 nil 结果,看看你传入的 NSError。

    【讨论】:

    • 有趣,会去看看。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多