2

I am working on a custom framework which will be used by developers. I am saving some data locally inside my framework for that I am thinking to use UserDefaults but I want to know:

  1. Is it the best method to do so?
  2. If the app UserDefaults are cleared then will it clear my frameworks data also?

I want to know how can I store local data in ios framework. What is the best solution for this? I am confused about it can anyone help?

Abhishek
  • 1,654
  • 2
  • 18
  • 31
  • You are just code that the host app is using. You have no “separate” storage or sandbox. – matt Feb 01 '20 at 14:36

2 Answers2

0

It depends on the data you want to store.

If your goal is to keep some settings for your framework (e.g. whether some features should be enabled or not) then UserDefaults will do. If you want to store user's data (or anything you can get from the server) then you should probably consider using CoreData, which is a native iOS/macOS/tvOS persistent storage.

You can also use Realm database, but it's not a good idea to use it in frameworks, since it will be added to any application which will use your framework (and the developers probably won't be happy about this).

Regarding you questions:

  1. It depends, see above
  2. Yes
Yury Bogdanov
  • 417
  • 7
  • 13
  • Thanks @yury for your feedback. Can downvoter explain the reason for downvote? – Abhishek Feb 01 '20 at 14:26
  • I want to store some framework settings depends upon user type. But Will the UserDefault will be deleted when the app user clear all UserDefault for example as most of the developers clear that on logout feature? – Abhishek Feb 01 '20 at 14:28
  • @Abhinshek Well, I think clearing such preferences on logout is a good thing, since you don't want the user that will log in to use your framework with previous user's preferences. And thus it is the app's responsibility to set your framework up after each logging in. They can also consider removing UserDefaults/clearing caches/whatever when a different user logs in (not after the user taps LogOut button). – Yury Bogdanov Feb 06 '20 at 18:25
0

Userdefaults is great but at times it turns into a mess if you have alot of information that needs to be stored. Specially the fact that you would need to manage a key/value relationship throughout the app and usually the keys end up being more than 10 plus ..

Just a suggestion but why not use CoreData to manage models and store data in sqlite db on device. When the framework is loaded you can check if the data exists if not then fetch it.

I would suggest using the functionality provided by apple natively rather than going for 3rd party solutions like Realm, with 3rd party you would need to upgrade to their latest version in order to make things work fine.

Please read these to clear any confusion. https://cocoacasts.com/what-is-the-difference-between-core-data-and-sqlite/

Tutorial on using Coredata: https://www.raywenderlich.com/7569-getting-started-with-core-data-tutorial

Muneeb Awan
  • 115
  • 1
  • 8