NSFetchedResultsController委托仅在谓词为nil时触发

时间:2013-07-18 19:44:38

标签: iphone ios nsfetchedresultscontroller

我一直在开发这个应用程序几个月,我们决定在其上实施核心数据。 我正在使用魔法记录,但这个问题在魔法记录之前已经存在。

应用程序加载一个包含fetchedResultsController的视图,同时我们将数据保存在后台线程中。

(不工作)

    - (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
    return fetchedResultsController;
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_user = %@",[prefs valueForKey:@"userID"]];
fetchedResultsController = [ICCall MR_fetchAllGroupedBy:nil
               withPredicate:predicate
                    sortedBy:@"id_Chamada"
                   ascending:NO
                   inContext:[NSManagedObjectContext MR_defaultContext]];

return fetchedResultsController;}

(工作 - 通知谓词设置为nil)

    - (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
    return fetchedResultsController;
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_user = %@",[prefs valueForKey:@"userID"]];
fetchedResultsController = [ICCall MR_fetchAllGroupedBy:nil
               withPredicate:nil
                    sortedBy:@"id_Chamada"
                   ascending:NO
                   inContext:[NSManagedObjectContext MR_defaultContext]];

return fetchedResultsController;}

如果您想知道我们在调用此方法的后台线程中插入数据:

    -(void)callWithID:(NSDictionary*)newCall{
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){
    ICCall *call = nil;

    call = [ICCall MR_findFirstWithPredicate:
            [NSPredicate predicateWithFormat:@"id_Chamada = %@",[newCall valueForKey:@"id_Chamada"] ]
                                   inContext:localContext];

    if(call)
    {
        // Call already Exists, update it?
    }
    else
    {
        call = [ICCall MR_createInContext:localContext ];
        call.id_Chamada = [newCall valueForKey:@"id_Chamada"];
        call.id_user = [newCall valueForKey:@"id_user"];
        call.designacao = [newCall valueForKey:@"designacao"];
        call.telefone = [newCall valueForKey:@"telefone"];
        call.data = [newCall valueForKey:@"data"];
        call.assunto = [newCall valueForKey:@"assunto"];
        call.mensagem = [newCall valueForKey:@"mensagem"];
        call.importancia = [newCall valueForKey:@"importancia"];
        call.ligar_volta = [newCall valueForKey:@"ligar_volta"];
        call.respondido = [newCall valueForKey:@"respondido"];
        call.respondido_data = [newCall valueForKey:@"respondido_data"];
        call.lido = [newCall valueForKey:@"lido"];
        call.eliminadoFO = [newCall valueForKey:@"eliminadoFO"];
        call.notas = [newCall valueForKey:@"notas"];
        call.favorito = [newCall valueForKey:@"favorito"];
        NSLog(@"--------------------------- CALL");
    }

} completion:^{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [[NSManagedObjectContext MR_defaultContext] save];
    }];
}];}

因此,如果我在fetchedResultsController中设置谓词,当我在后台插入信息时,fetchedResultsController委托将不会触发,如果没有设置谓词,则所有fetchedResultsController委托方法都会正确触发。

这个问题一直存在,因为在MagicalRecord包装之前,我甚至尝试过其他包装但无济于事。 自上周以来一直试图解决这个问题,用完选项...... 一位朋友建议我不要使用线程,但我真的不想这样做。 建议?

1 个答案:

答案 0 :(得分:0)

在此链接中找到解决方案:https://groups.google.com/forum/#!topic/restkit/O2gy7h6cCwA

我希望这有助于其他人!