在查找或创建循环期间将数据追加到NSFetchedResultsController [英] Appending data to NSFetchedResultsController during find or create loop

查看:78
本文介绍了在查找或创建循环期间将数据追加到NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由NSFetchedResultsController管理的表格视图。但是,我在查找或创建操作时遇到问题。当用户点击表格视图的底部时,我正在向服务器查询另一批内容。如果本地缓存中不存在它,我们将创建并存储它。但是,如果确实存在,我想将该数据追加到获取的结果控制器中并显示它。

I have a table view that is managed by an NSFetchedResultsController. I am having an issue with a find-or-create operation, however. When the user hits the bottom of my table view, I am querying my server for another batch of content. If it doesn't exist in the local cache, we create it and store it. If it does exist, however, I want to append that data to the fetched results controller and display it. I can't quite figure that part out.

这是到目前为止我正在做的事情:

Here's what I'm doing thus far:


  1. NSFetchedRequestController初始化查询数据库中的最新100个结果时(使用 setFetchLimit:)。即使有1000行,我一开始也只希望访问100条。

  2. 将返回的值数组从服务器传递到NSOperation进行处理。

  3. 在操作中,创建一个要使用的新托管对象上下文。

  4. 在该操作中,我遍历数组并执行获取请求查看对象是否存在(基于其服务器ID)。

  5. 如果对象不存在,我们将其创建并将其插入操作的托管对象上下文中。

  6. 在迭代完成之后,我们保存了托管对象上下文,这会在我的主线程上触发合并通知。

  1. The NSFetchedRequestController when initialized queries for the latest 100 results from the database (using setFetchLimit:). Even if there are 1000 rows, I only want 100 accessible at first.
  2. Passing the returned array of values from my server to an NSOperation to process.
  3. In the operation, create a new managed object context to work with.
  4. In the operation, I iterate through the array and execute a fetch request to see if the object exists (based on its server id).
  5. If the object doesn't exist, we create it and insert it into the operations' managed object context.
  6. After the iteration completes, we save the managed object context, which triggers a merge notification on my main thread.

在合并期间,将第4步中新创建的对象插入到表中,但是任何已存在且刚刚被提取的对象才不是。这是我的NSOperation中的相关代码

During the merge, the newly created objects from step 4 are inserted into the table, but any object that already existed and was just fetched does not. Here's the relevant code from my NSOperation

for (NSDictionary *resultsDict in self.results)
{
    NSNumber *dbID = [NSNumber numberWithLong:[[resultsDict valueForKey:@"id"] longValue]];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:kResultEntityName inManagedObjectContext:moc]];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"(dbID == %@)", dbID]];

    NSError *error = nil;

    NSManagedObject *newObject = nil;
    // Query the data store to see if the object exists
    newObject = [[moc executeFetchRequest:fetchRequest error:&error] lastObject];
    [fetchRequest release];

    // If it doesn't exist, create it.
    if ((tweet == nil))
    {
        // Create the NSManagedObject and insert it into the MOC.  
    }
}

我想传递给主线程的是每次循环时,新创建的对象以及可能已在获取请求中获取的任何现有对象。

What I want to pass to my main thread is the newly created objects plus any existing objects that may have been fetched in the fetch request each time the loop iterates.

我觉得这很简单,我很想念它,可以朝正确的方向轻推。

I feel like it's something simple I'm missing, and could use a nudge in the right direction.

推荐答案


此时,之前未在本地存储在我的Core Data存储中的任何对象会出现,但是以前存在的不会出现。

At this point, any objects that weren't locally cached in my Core Data store before will appear, but the ones that previously existed do not come along for the ride.

您能解释一下吗?我不确定您所说的是先前存在的意思。是检索后现在执行的与表过滤器不匹配的对象,还是您说先前的对象从表中消失了?

Can you explain this a little? I am not sure what you mean by the ones that previously existed. Is it objects that didn't match the filter on your table that now do after the retrieval or are you saying that the previous objects disappear from the table?

您是否在 NSFetchedResultsController 中实现委托方法?您是在执行简单的表重新加载还是要插入/移动行?

Also, how are you implementing the delegate methods for the NSFetchedResultsController? Are you doing a simple table reload or are you inserting/moving rows?

有几个的方法,但是您需要进行一些实验。我将首先尝试触摸获取的对象。也许更新 lastAccessed日期或其他内容,以使这些对象以更新的方式通过合并。我怀疑这是最简单的路径。

There are a couple of ways to do this but they would require some experimentation on your part. I would first play with the idea of "touching" the objects that were fetched. Perhaps updating a 'lastAccessed' date or something so that these objects will come across the merge as "updated". I suspect that is the easiest path.

除非,另一种选择是将这些对象的通知广播回主线程​​(使用它们的 NSManagedObjectID 作为跨线程边界的载体),以便您可以手动更新它们;但是那不理想。

Baring that, another option would be to broadcast a notification of those objects back to the main thread (using their NSManagedObjectID as the carrier across the thread boundaries) so that you can manually update them; however that is less than ideal.

这篇关于在查找或创建循环期间将数据追加到NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆