简体   繁体   中英

Should I use NSUserDefault, dictionaries, core data - or something else?

I'm having some issues with the app, that I'm making, which I thought would be a lot easier to explain with some photos, so ... :

Ofcourse the "Create New Person-button" in nr. 1 leads you to number two.

Now, I'm having issues figuring out how to save this data about the person in the "People Diary". The goal is, that when you enter a person's name, add a photo (an enable-camera feature, I will struggle with at a later time...) and add an answer to the question - then you only need to press "Save this person", and then you will be redirected to the AllPersonsInYourDiaryViewController, where there is now a new tableViewCell with this new person's name (maybe with a subtitle containing the answer and the photo shown in miniature in the cell too).

(Naturally you can then enter this cell with the data about the person too - but that comes next.)

So far in the app, I have used NSUserDefault, when allowing the user to create this specifik Diary by the Name "Antons Diary" with the specifik question and so on. But now it came to my attention, that maybe it is smarter to use something else? I tried with dictionaries, but couldn't get this to work properly.

So...: Before I spend hours and hours playing around with one of these ways, will someone smarter than me, tell me what the best approach would be?

I can give you the logic behind coreData and NSUserDefaults, but you will decide which one should be used.

CoreData is usually used as a database. you can create entities and attributes for every entity. Moreover, you can create relations between these entities. When extracting data from coreData, you can arrange this data using NSSortDescriptor or select a specific record using NSPredicate . So as you can see CoreData is a database.

While NSUserDefaults is usually used to save a password, username, userID... and such issues that you will regularly use in the app. NSUserDefaults gives you a direct access to the saved variables at any time. However, CoreData will take more time and lines of code to access the entity and make the query.

Now, check which method suits your case more.

If I can give my two cents, the first thing you have to do is to “design” how to represent a person programmatically. You can create a struct or class to do so, even though a struct is more suitable:

struct Person {
    var name: String?
    var answer: String?
    var photo: String?
}

Then you can decide how to save the data of such an object persistently. If you want to use a database, then I would recommend using SQLite with FMDB library . It's really easy and fast to learn how to use it, and it's also quite handy. I've used it big projects and it works smoothly. I find CoreData too complicated and an overkill based on what you need.

If you don't want to use a database, your only other way is to save to files, but still, you've got options here too. If you encode (see Codable protocol in Swift), you can use NSKeyedArchiver to convert to Data object and write then to disk. If you like using dictionaries, and since the properties you're going to have for a person are not going to be too many, you could create a dictionary by assigning the properties and their values, and then convert and save as JSON data, or even Plist files. Without any intension to do promotion here, but just to provide some additional help, if you want take a look to a library that I've written and that can do all these automatically for you. It's a protocol that you have to adopt, and then you can instantly convert your struct to a dictionary, JSON or plist and save to files.

No matter which way you're going to select, save the images as single files to documents directory, and keep their file names only stored to database/file. Based on them, you can build the path to each image (or the URL) easily when needed. Warning: Do not save the full path to the documents directory, especially if you're testing on Simulator; paths are changing on each build. Save the file name only.

Additionally, if you're going to use a struct like the one shown above, you could implement small but super convenient functions that will be responsible for saving, loading, or updating your data to the solution (database/file) you'll eventually select. That way, you'll have related stuff gathered in one place, and easily accessible (ie, person.save()).

struct Person {
    var name: String?
    var answer: String?
    var photo: String?

    func save() {
        …
    }

    func load() {
        …
    }

    // More functions…
}

Lastly, avoid using UserDefaults, or at least keep just a few non-critical data there. UserDefaults are not meant to keep all data produced by your app. Most importantly, do not use it for saving sensitive data, especially passwords or other stuff like that.

I hope the above will help you make your mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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