如何同步Core Data关系?

发布于 2024-12-02 20:20:54 字数 311 浏览 0 评论 0原文

我正在创建一个应用程序,它从 Web 服务器 (MySQL) 提取数据,解析它并使用 Core Data 将其存储在 SQLite 数据库中。

MySQL 数据库有一个“words”表。每个单词都可以属于一个“类别”。因此,words 表有一个用于连接表的“category_id”字段。

我在思考如何在我的应用程序中本地复制此内容时遇到一些困难。我目前有与 MySQL 数据库结构匹配的实体,但没有关系。似乎在我的“单词”实体中,我不需要“category_id”字段(我应该设置一对一的“类别”关系)。

我对如何保持核心数据关系与网络服务器同步感到困惑?

I'm creating an app that pulls data from a web server (MySQL), parses it and stores it in a SQLite database using Core Data.

The MySQL database has a 'words' table. Each word can be in a 'category'. So the words table has a field for 'category_id' to join the tables.

I'm having some trouble getting my head around how to replicate this locally in my app. I currently have entities matching the structure of the MySQL database, but no relationships. It seems like in my 'words' entity I shouldn't need the 'category_id' field (I should instead have a one-to-one 'category' relation set-up).

I'm confused as to how to keep this Core Data relationship in sync with the web server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-12-09 20:20:54

假设您有一个用于 WordCategoryEntity,您将需要建立一种关系(命名可能有点模糊)。另外假设一个类别可以有很多单词,并且

// Word Entity
Relationship    Destination    Inverse
category        Categories     words

// Category Entity
Relationship    Destination   Inverse
words           Word          category       // To-Many relationship

您是对的,您不需要category_id字段,因为所有关系都是通过核心数据维护的对象图进行管理的。您仍然需要在每个实体中使用像 server_id (或类似的)这样的主键,否则您将无法更新/查找已保存的对象。

这就是我处理从外部数据库同步数据的方式(我使用带有 JSON 的 RESTful 接口,但这并不重要)

  1. 获取按 server_id 排序的提要
  2. 获取提要中所有对象的主键 (server_id)
  3. 使用以下命令执行提取像 ... @"(serverId IN %@)", PrimaryKeys 这样的谓词
    这是按主键排序的。
  4. 逐步遍历每个数组。如果获取结果有我的记录,那么我会更新它。如果没有,那么我插入一个新的。
  5. 您需要对 WordCategory 执行此操作
  6. 接下来获取构成关系一部分的所有对象
  7. 使用核心数据生成的适当方法来添加对象。例如类似`[myArticle addWords:[NSSet setWithObjects:word1, word2, word3, nil];

我很难测试,但这应该给你一个起点?
很高兴看到 Shiny 课程的参加者使用堆栈溢出 - 不仅仅是我

Assuming you have an Entity for Word and Category you will need to make a relationship (naming may be a bit hazy). Also assuming a Category can have many words and

// Word Entity
Relationship    Destination    Inverse
category        Categories     words

// Category Entity
Relationship    Destination   Inverse
words           Word          category       // To-Many relationship

You are correct you would not need the category_id field as all relationships are managed through the object graph that Core Data maintains. You will still need a primary key like server_id (or similar) in each entity or you will have trouble updating/finding already saved objects.

This is how I deal with syncing data from an external database (I use RESTful interfaces with JSON but that does not really matter)

  1. Grab the feed sorted by server_id
  2. Get the primary keys (server_id) of all the objects in the feed
  3. Perform a fetch using the a predicate like ... @"(serverId IN %@)", primaryKeys
    which is sorted by the primary key.
  4. Step through each array. If the fetch result has my record then I update it. If it does not then I insert a new one.
  5. You would need to do this for both Word and Category
  6. Next fetch all objects that form part of a relationship
  7. Use the appropriate methods generated by core data for adding objects. e.g. something like `[myArticle addWords:[NSSet setWithObjects:word1, word2, word3, nil];

It's hard for me to test but this should give you a starting point?
Good to see a fellow Shiny course attendee using stack overflow - it's not just me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文