#codeindia

Pubblicato il: 19 aprile 2022
sul canale di: #codeindia
298
15

#codeindia


iOS Data Persistence in Swift
 Data persistence is the mechanism of storing any type of data to disk so that the same data can be retrieved without being altered when the user opens the app next time. While you could save the same data on a server, in this tutorial we are going to describe all the ways to store data locally in iOS apps.

By default, all the variables and constants that you allocate in Swift are stored in memory, so they get lost when users quit the app. Different types of data can be saved to the device locally, using different tools (Swift APIs). To achieve that in Swift that we can use the following technologies (Swift APIs):
UserDefaults
Keychain
Saving files to disk
Core Data
SQLite
Property Lists


UserDefaults
UserDefaults can save integers, booleans, strings, arrays, dictionaries, dates and more, but you should be careful not to save too much data because it will slow the launch of your app.

Advantages
UserDefaults is easy to use, with a simple API
Thread safe (you can read and write values from any thread), without worrying about synchronization
UserDefaults is shared between the app and the app extensions
Limitations
It is possible to easily override the value for this same key (key collisions)
UserDefaults is not encrypted
Unit testing user defaults can occur with some false positives.
UserDefaults can be altered globally, from anywhere in the app, so you can run in inconsistent states easily



Keychain
 It can be used to save passwords, secure notes, certificates, etc. In general, Keychain is an encrypted database with quite a complicated and robust API.
when you need to save sensitive user data, such as passwords or login credentials. For example, we are using Keychain to implem

Advantages
All items saved in keychain are encrypted (Keychain is the most secure data persistence layer in iOS)
There is a lot of open source wrappers with user-friendly Swift APIs
Once again security
Thread safety
Limitations
There is no handy and old-fashioned Keychain API, so you usually end up depending on an open-source project
Hard to unit test, unless you create your own wrapper around it
Speed: Reading or saving a lot of information into Keychain can be slow
Not recommended for storing large objects

CoreData
Core Data Advantages
CoreData is simple to set up and use
Lots of additional tools provided by Apple for fetching data (https://developer.apple.com/documenta...)
Speed & Performance: Possibility to be a cache when setup `in memory `
Out-of-the-box database migrations (simple one)
Core Data Limitations
Quite advanced threading policy
Being forced to use NSManagedObject subclasses for every new object in the database
Sometimes NSManagedObjectContext have problems with synchronizing database changes

Saving Files to Disk
Apple makes writing, reading, and editing files inside the iOS applications very easy. Every application has a sandbox directory (called Document directory) where you can store your files. FileManager object provides all these functionalities with very simple APIs.
Files can be stored inside catalogs and sub-catalogs of your app’s Document directory. That’s why Apple recommends using the URL to specify and work with files using the FileManager object.

Out-of-the-box database migrations

SQLite
SQLite is a relational database management system contained in a C programming library. In contrast to many other database management systems, SQLite is not a client-server database engine. In fact, it is embedded in the client application itself. So there is no networking involved in SQLite.

Property list (Plists) in Swift
Plist files enable storing serialized objects with key/value conventions. In macOS and iOS applications, the most common usage example of plist is an Info.plist file which stores application settings values. Inside Info.plist we can find the application name, bundle identifier, version number, required permissions info, Facebook App info, etc. Another example of a .plist file, implemented in all of our iOS App Templates that use Firebase, is the GoogleService-Info.plist file, that contains all the Firebase API Keys, needed for user authentication & database access.


In questa pagina del sito puoi guardare il video online #codeindia della durata di ore minuti seconda in buona qualità , che l'utente ha caricato #codeindia 19 aprile 2022, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 298 volte e gli è piaciuto 15 spettatori. Buona visione!