我正在使用MagicalRecord,但不确定这是否是MagicalRecord或CoreData中的错误。

当用户单击新按钮并将其保存到默认上下文时,我在UI线程中创建了新实体:

- (IBAction) saveToCoreData:(UIButton *)sender {
    NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
    Report *report = [Report MR_createInContext:context];
    report.dirty = [NSNumber numberWithBool:YES];
    report.timestamp = [NSDate date];

    Document *document = [Document MR_createInContext:context];
    document.dirty = [NSNumber numberWithBool:YES];
    document.timestamp = [NSDate date];
    document.report = report;

    NSLog(@"Saving.................................");
    [context MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        NSLog(@"Saved report and document to PS");
    }];
}

注意我有一个带有子文档的报告实体

然后,我有一个后台线程仅负责监听更改,如果Report更改,dirty == YES,则需要将其提交到服务器。

我在后台线程上设置了NSFetchedResultsController来监听更改,因此我知道何时将其保存到服务器。
// This should get me a new background context, use it in FRC below
    self.fetchedResultsController = [Report MR_fetchAllSortedBy:@"timestamp"
                                                      ascending:NO
                                                  withPredicate:[NSPredicate predicateWithFormat:@"dirty == YES"]
                                                        groupBy:nil
                                                       delegate:self
                                                      inContext:_managedObjectContext];

然后,我实现了监听更改的方法:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id) anObject atIndexPath:(NSIndexPath *) indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *) newIndexPath {
    switch(type) {

        case NSFetchedResultsChangeInsert:
            NSLog(@"report inserted");
            [self pushReport:anObject];
            break;
        case NSFetchedResultsChangeUpdate:
            NSLog(@"report updated");
            [self pushReport:anObject];
            break;
    }
}

并将其推送到服务器:
- (void) pushReport:(Report *) report {
    __weak ReportPushService *weakSelf = self;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://echo.jsontest.com/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [weakSelf.managedObjectContext performBlockAndWait:^{
            report.remoteId = @"12345"; // fake id from server
            report.dirty = [NSNumber numberWithBool:NO];

            [weakSelf.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
                NSLog(@"Saved server info to Report");
            }];
        }];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
     }];
}

FRC就像一个 super 按钮一样工作,当UI线程添加带有脏== YES的新报告时,FRC会选择它并将其提交给服务器。

这是我遇到问题的地方。将报告保存到服务器后,服务器将返回该报告的ID。我将其设置为report.remoteId。

我在另一个类中有一个FRC,负责将文档保存到服务器。它将检查脏的文档==是,并且其父(报表)remoteId!= nil,即:
    self.fetchedResultsController = [Document MR_fetchAllSortedBy:@"timestamp"
                                                      ascending:NO
                                                  withPredicate:[NSPredicate predicateWithFormat:@"report.remoteId != nil && dirty == YES"]
                                                        groupBy:nil
                                                       delegate:self
                                                      inContext:_managedObjectContext];

它使用与报告“推送”类中的FRC Controller 相同的后台MOC。

然后,我收听该FRC的更改:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id) anObject atIndexPath:(NSIndexPath *) indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *) newIndexPath {
    switch(type) {
        case NSFetchedResultsChangeInsert:
            NSLog(@"document inserted");
            [self pushDocument:anObject];
            break;
        case NSFetchedResultsChangeUpdate:
            NSLog(@"document updated");
            [self pushDocument:anObject];
            break;
    }
}

但是,即使我已经验证我有一个remoteID!= nil的报告和一个肮脏的== YES的(子)文档,他的FRC都看不到任何变化。

为什么文档FRC没有看到与该谓词匹配的对象?

最佳答案

FRC的问题/局限性在于它仅包含为其设置的实体。在这种情况下:

self.fetchedResultsController = [Document MR_fetchAllSortedBy:@"timestamp"
                                                      ascending:NO
                                                  withPredicate:[NSPredicate predicateWithFormat:@"report.remoteId != nil && dirty == YES"]
                                                        groupBy:nil
                                                       delegate:self
                                                      inContext:_managedObjectContext];

FRC仅包含文档实体。初始提取将对报表关系起作用。但是,如果谓词匹配,则即使报表更改,FRC也不会更新。

http://www.mlsite.net/blog/?p=825

和这里

Changing a managed object property doesn't trigger NSFetchedResultsController to update the table view

关于ios - 更改无法识别我的NSFetchedResultsController附加到后台线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27517396/

10-08 21:25