Core Data多线程存储关系 [英] Core Data multithread store relations

查看:144
本文介绍了Core Data多线程存储关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很奇怪,我要求帮助。我想向你解释我的系统是如何工作的。我有两个问题。



我有两个表的应用程序。我使用核心数据。表格是 的产品,在核心数据中,我建立了一对多的关系 可以有多个


$ b

code>类别{
site_id
name
allProducts - >>产品
}

产品{
site_id
name
category - >类别
}




  • 这是简单的模型产品和catwegories更多属性



在该应用程序中,我有更新过程,该过程在backgournd线程中工作。我已经为该线程创建了上下文,并且在该上下文中存储了所有更改。



所有caluclation在后台线程中执行

  [context performBlock:^ {
//更新存储类别和产品的所有操作
}]

这是更新过程的流程


  1. 从服务器获取带类别数据的JSON

  2. 从服务器获取带有产品数据的JSON


  3. 传递类别JSON和商店类别实体,我从JSON获取名称和site_id并创建实体并设置这些属性


  4. 从JSON的属性创建产品实体并设置名称和store_id


/ p>


  1. 现在,我需要根据第3步中获取的JSON将产品连接到类别。这是一个问题。

我从JSON中获取foreach类别,并将所有产品site_id作为NSArray,我使用带谓词和IN标准的fetch请求获取所有产品具有site_id IN数组的上下文实体。但我总是得到零结果。这是奇怪的,因为我在后台线程的上下文调用fetch结果,我也更新了上下文的步骤3和4。



然后我尝试下一件事,在步骤6 I [上下文保存:&错误] 并添加睡眠约30秒,当该程序运行到步骤6之后,所有工作funtastic我的读取结果得到具有IN条件谓词。这是我的第一个问题,如果你明白我,我不能得到产品通过ID我需要等待大约30秒到上下文存储到SQLLiteDB它是非常奇怪。这也是奇怪的,因为当我想获得类别与fetch请求通过id我可以得到它没有问题 (也许,这是因为步骤5需要大约一分钟)。



我的下一个问题是当我等待30秒,然后通过trought类别。基于类别site_id我从上下文中获取类别实体,并且在步骤6中,我基于通过JSON和获取请求获得的产品site_ids获得所有产品,现在我想将产品添加到类别,然后调用下一个



[category setAllProducts:results] //结果是NSSet的产品,类别是实体对象



NSError * error = nil;
[context save:& error];



我执行foreach categroy(在JSON格式的服务器的第3步中获取)



当所有操作完成后,我在UI上看不到categroies中的产品我也等待一段时间所有的数据都存储在DB中,但没有什么heppenes,



然后,当我停止应用程序并运行它,我可以看到类别中的产品:(



很奇怪的问题,请帮我我不知道在哪里



非常感谢你

$

b $ b

解决方案

我的理解是你正在使用子上下文。子上下文受到许多( many!



/wbyoung.tumblr.com/post/27851725562/core-data-growing-pains\">http://wbyoung.tumblr.com/post/27851725562/core-data-growing-pains



我的建议是使用直接连接到商店协调器的上下文,而不是子上下文。一切都将正常使用这样的设置。你当然可以使用私有队列上下文。


my problem is very strange and I am asking for help. I want to explain to you how my system works. I have two problem with this.

I am making application which have two tables. I am using core data. Tables are Categories and Products, in core data I have established one to many relationship Category can have multiple Products, Product can hove one Categroy.

Here is basic schema

Category {
  site_id
  name
  allProducts ->> Product
}

Product {
  site_id
  name
  category -> Category
}

  • this is simplefied model products and catwegories has more properties

In that application I have updating process, and that process works in backgournd thread. I have created context for that thread and all changes I am storing in that context.

All caluclation I am executing in background thread in

[context performBlock:^{
  // all my operations for updating storing categories and products
}]

This is flow of updateding process

  1. From server get JSON with categories data
  2. From server get JSON with products data
  3. From server get JSON with info which category has which products array((category_site_id > array(product_site_id1, product_site_id2...))

  4. Pass through categories JSON and store category entity, I am getting name and site_id from JSON and create entity and set these properties

  5. Pass through products JSON and create product entity and set name and store_id from properties from JSON

Then main part

  1. Now I need to connect product to categories based on JSON geted in step 3 and here is a problem.

I am going foreach category from JSON and get all products site_id as NSArray, I am using fetch request with predicate and IN criteria to get all products entities from context which have site_id IN array. But I always got zero results. That is strange because I am calling fetch results on context in background thread and I also updated that context in step 3 and 4.

Then I try next thing, before step 6 I [context save:&error] and added sleep about 30 secconds and when after that program run into step 6 all work funtastic my fetch results get products with predicate with IN criteria. That is a my first problem if you understand me, I can not get products by IDs I need to wait about 30 sec to context store to SQLLiteDB it is very strange. Also this is strange because when I want to get category with fetch request by id I can get it without problems (maybe that it is because step 5 takes about one minute).

My next problem is when I wait that 30 sec and then passing trought categories. Based on category site_id I get Category entity from context and also in step 6 I get all products base on product site_ids geted by JSON and fetch request and now I want to add products to category and I call next

[category setAllProducts:results] // results are NSSet of product, category is entity object

NSError *error = nil; [context save:&error];

and I do that foreach categroy (geted in step 3 from server in JSON format)

when all is done I can not see products in categroies on UI I am also waiting some time to all data be stored in DB but nothing heppenes,

AND THEN WHEN I STOP APPLICATION AND RUN IT AGAIN I CAN SEE PRODUCTS IN CATEGORIES :(

very strange problem please help me I do not know where I am a wrong. I can post parts of code, code is not small and I asking for help based on description of process.

Thank you very much

解决方案

My understanding is that you are using child contexts. Child contexts are affected by many (many!) bugs you have to deal with.

Pretty good article about that topic here:

http://wbyoung.tumblr.com/post/27851725562/core-data-growing-pains

My advice on that matter is to use a context directly connected to the store coordinator, not a child context. Everything will work fine using such a setup. You can use a private queue context of course.

这篇关于Core Data多线程存储关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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