我正在使用 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 的工作就像一个魅力,当 UI 线程添加一个带有 dirty == YES 的新报告时,FRC 将其拾取并提交给服务器。
这是我遇到问题的地方。将报告保存到服务器后,服务器会返回该报告的 ID。我将其设置为 report.remoteId。
我在另一个类中有一个FRC,负责将文档保存到服务器。它将检查文件是否脏 == YES 以及它的父级(报告)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;
}
}
但是他的 FRC 从未看到任何变化,即使我已经验证我有一个带有 remoteId 的报告!= nil 和一个(子)文档带有 dirty == YES。
为什么 Documents 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
这里
关于ios - 更改无法识别我附加到后台线程的 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517396/