2

我已经开发这个应用程序几个月了,我们决定在它上面实现 Core Data。我正在使用 Magical Record,但这个问题在 Magical Record 之前就已经存在。

应用程序加载具有 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;}

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

    - (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 包装器之前一直存在,我什至尝试了其他包装器但无济于事。自上周以来一直在尝试解决此问题,但选项不多了……一位朋友建议我不要使用线程,但我真的不想那样做。建议?

4

0 回答 0