gpt4 book ai didi

ios - 无法理解何时在 Coredata 中使用 int 或 string

转载 作者:行者123 更新时间:2023-11-30 12:26:18 24 4
gpt4 key购买 nike

iOS 10、Swift3

我正在制作一个应用程序,用户可以从服务器获取他们的每日议程,我想将他们的日程安排保存在 Coredata 中。来自 API 的简单 json 类似于

{
"id": 11639002,
"subject": "Coffeee",
"startUTC": "2017-05-03T15:00:00+00:00",
"endUTC": "2017-05-03T16:00:00+00:00"
}

如果在服务器端进行更改,我想更新约会,我也不想保存重复的条目。另外,我希望能够按 startDate 对此行进行排序,我想按其 id

传递此约会行

我认为一个好的解决方案是这样的

@NSManaged public var id: Int32
@NSManaged public var subject: String?
@NSManaged public var startUTC: NSDate?
@NSManaged public var endUTC: NSDate?

我在这里读到的每一篇文章都说 Coredata 不是 ORM 或关系数据库,因此开发人员应该摆脱这种心态。

当我尝试像在服务器端那样复制 unique_id 方法时,我不明白如何以及为什么需要摆脱关系数据库思维模式。

本教程 http://dorianroy.com/blog/2015/09/how-to-implement-unique-constraints-in-core-data-with-ios-9/

这么说

No more fetch/if/else

Until now, when you wanted to import data objects into Core Data from a file or network request, you had to create a fetch request for each incoming object with a predicate that matches the id and then execute it to look for an existing version of that object. If you found one, you would update it, otherwise create a new object. With Unique Constraints, you don’t have to do this fetch/if/else anymore and save lots of DB requests while parsing the data.

唯一约束必须是字符串

所以我想做的就是不要有重复的行并通过它的 id 更新该行。

我应该使用什么方法?我是否必须像该博客文章中那样制作 id 字符串,或者旧的常规 32 位整数方法也可以工作?

最佳答案

最重要的是,您应该编写您理解的代码,并且以其他人第一次看到它时也能理解的方式编写代码。确实,核心数据不是关系数据库,但它是关系数据库的包装器。因此,如果您可以使用该范式解决您的问题,那么它很简单且可以理解,我说那就去做吧。准备好在未来某个地方重新审视您的实现。但到那时你会更好地理解你的问题。

关于ios - 无法理解何时在 Coredata 中使用 int 或 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44166145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com