ios - 更改无法识别我的 NSFetchedResultsController 附加到后台线程
<p><p>我正在使用 MagicalRecord,但不确定这是 MagicalRecord 还是 CoreData 中的错误。</p>
<p>当用户单击新按钮并将其保存到默认上下文时,我会在我的 UI 线程中创建新实体:</p>
<pre><code>- (IBAction) saveToCoreData:(UIButton *)sender {
NSManagedObjectContext *context = ;
Report *report = ;
report.dirty = ;
report.timestamp = ;
Document *document = ;
document.dirty = ;
document.timestamp = ;
document.report = report;
NSLog(@"Saving.................................");
[context MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
NSLog(@"Saved report and document to PS");
}];
}
</code></pre>
<p>请注意,我有一个附有子文档的报告实体</p>
<p>然后我有一个后台线程只负责监听变化,如果一个报告发生变化,dirty == YES 那么它需要被提交到服务器。</p>
<p>我在后台线程上设置了 NSFetchedResultsController 来监听更改,以便知道何时保存到服务器。</p>
<pre><code>// This should get me a new background context, use it in FRC below
self.fetchedResultsController = [Report MR_fetchAllSortedBy:@"timestamp"
ascending:NO
withPredicate:
groupBy:nil
delegate:self
inContext:_managedObjectContext];
</code></pre>
<p>然后我实现监听变化的方法:</p>
<pre><code>- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id) anObject atIndexPath:(NSIndexPath *) indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *) newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
NSLog(@"report inserted");
;
break;
case NSFetchedResultsChangeUpdate:
NSLog(@"report updated");
;
break;
}
}
</code></pre>
<p>并将其推送到服务器:</p>
<pre><code>- (void) pushReport:(Report *) report {
__weak ReportPushService *weakSelf = self;
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 = ;
[weakSelf.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
NSLog(@"Saved server info to Report");
}];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
</code></pre>
<p>FRC 的工作原理就像一个魅力,当 UI 线程添加一个带有dirty == YES 的新报告时,FRC 会拾取它并将其提交给服务器。</p>
<p>这是我遇到问题的地方。将报告保存到服务器后,服务器会返回该报告的 ID。我将其设置为 report.remoteId。</p>
<p>我在另一个类中有一个 FRC,负责将文档保存到服务器。它将检查文档脏 == YES 并且它的父(报告)remoteId != nil,即:</p>
<pre><code> self.fetchedResultsController = [Document MR_fetchAllSortedBy:@"timestamp"
ascending:NO
withPredicate:
groupBy:nil
delegate:self
inContext:_managedObjectContext];
</code></pre>
<p>它使用与报告“推送”类中的 FRCController 相同的背景 MOC。</p>
<p>然后我监听该 FRC 的变化:</p>
<pre><code>- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id) anObject atIndexPath:(NSIndexPath *) indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *) newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
NSLog(@"document inserted");
;
break;
case NSFetchedResultsChangeUpdate:
NSLog(@"document updated");
;
break;
}
}
</code></pre>
<p>但是他的 FRC 从来没有看到任何变化,即使我已经确认我有一个 remoteId != nil 的报告和一个脏 == YES 的(子)文档。</p>
<p>为什么 Documents FRC 看不到与该谓词匹配的对象?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>FRC 的问题/限制是它只包含为其设置的实体。在这种情况下:</p>
<pre><code>self.fetchedResultsController = [Document MR_fetchAllSortedBy:@"timestamp"
ascending:NO
withPredicate:
groupBy:nil
delegate:self
inContext:_managedObjectContext];
</code></pre>
<p>FRC 将仅包含文档实体。初始提取将作用于报告关系。但如果报告发生变化,即使谓词匹配,FRC 也不会更新。</p>
<p> <a href="http://www.mlsite.net/blog/?p=825" rel="noreferrer noopener nofollow">http://www.mlsite.net/blog/?p=825</a> </p>
<p>这里</p>
<p> <a href="https://stackoverflow.com/questions/12370521/changing-a-managedobject-property-doesnt-trigger-nsfetchedresultscontroller-to" rel="noreferrer noopener nofollow">Changing a managed object property doesn't trigger NSFetchedResultsController to update the table view</a> </p></p>
<p style="font-size: 20px;">关于ios - 更改无法识别我的 NSFetchedResultsController 附加到后台线程,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27517396/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27517396/
</a>
</p>
页:
[1]