简体   繁体   English

多用户的最佳实践:核心数据或NSUserDefaults

[英]Best practice for multiple users: Core Data or NSUserDefaults

I'm developing an app that can have multiple users. 我正在开发一个可以拥有多个用户的应用。 I mean, when a user downloads the app, he'll have 1 user ( default ), but he can add more users for his parents/sons/wife/etc. 我的意思是,当用户下载应用程序时,他将拥有1个用户(默认),但他可以为他的父母/儿子/妻子/等添加更多用户。 And each user will have his own data. 每个用户都有自己的数据。

So my question is: What is the best way to handle these users' data ? 所以我的问题是:处理这些用户数据的最佳方法是什么? I was thinking about using Core Data but i 'think' it's really complicated to add a new attribute to the Users entity ( i'm not sure! ). 我正在考虑使用Core Data,但我认为向User实体添加一个新属性真的很复杂(我不确定!)。 So, now i'm thinking about using NSUserDefaults ( this might be less complicated.. ). 所以,现在我正在考虑使用NSUserDefaults(这可能不那么复杂......)。

I hope you can help me with this, even if i have to use/do something else. 我希望你能帮助我,即使我必须使用/做别的事情。

Thank you, Bader Al-Rasheed 谢谢你,Bader Al-Rasheed

Do not use NSUserDefaults for data. 不要将NSUserDefaults用于数据。 Maybe one string here and there, but NEVER an entire data model. 也许这里和那里有一个字符串,但绝不是整个数据模型。

As for building this with Core Data, it is fairly straightforward. 至于使用Core Data构建它,它是相当简单的。 You need to create a User entity with all necessary attributes and then you can create, insert, update, and delete your records using standard Core Data. 您需要创建具有所有必需属性的用户实体,然后您可以使用标准Core Data创建,插入,更新和删除记录。

I made an example application that uses CoreData. 我做了一个使用CoreData的示例应用程序。 Feel free to check it out: https://github.com/Jakenberg/CoreDataExample 请随意查看: https//github.com/Jakenberg/CoreDataExample

Is there any overlap between users' data? 用户数据之间是否有任何重叠? If not, then use a separate datastore for each user. 如果没有,则为每个用户使用单独的数据存储。 You could use NSUserDefaults to keep track of who the current user is, and then point your NSPersistentStoreCoordinator to the appropriate file when the user changes. 您可以使用NSUserDefaults跟踪当前用户的身份,然后在用户更改时将NSPersistentStoreCoordinator指向相应的文件。 Use a different URL for each user in the call to NSPersistentStore 's initWithPersistentStoreCoordinator:configurationName:URL:options . 在调用NSPersistentStoreinitWithPersistentStoreCoordinator:configurationName:URL:options为每个用户使用不同的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM