我正在使用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");
}];
}
注意我有一个附有子文档的报告实体
然后我有一个后台线程,只负责监听更改,如果报告发生变化,脏==是,那么它需要提交给服务器。
我在后台线程上设置了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线程添加一个带有脏的新报告== 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控制器相同的背景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的报告和一个带有脏== YES的(子)文档。
为什么Documents FRC没有看到与该谓词匹配的对象?
答案 0 :(得分:1)
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
在这里