Data persistence is an important function in iOS development. There are times when we have to work without an Internet connection, which is where offline capabilities of a mobile app come into front. Local storage is meant for retaining web app data locally using certain frameworks, tools and methods distinctive to different platforms. For iOS storage there are different methods to choose from. The choice, however, depends upon what and how much data you want to store. Most of the times more than one method is required to implement local storage in iOS apps, as there are different persistence needs of the application viz. data gathered from web browsing, user preferences, and application settings.

Database Selection criteria:-

  • Relational Database (SQLite):-

 SQLite is a library that implements a lightweight embedded relational database. As its name implies, it’s based on the SQL standard (Structured Query Language) .The main difference with other SQL databases is that SQLite is portable, very lightweight, and that it’s server less instead of a separate process accessed from the client application. In other words, it’s embedded in the application and therefore very fast.

The advantage SQLite has over working directly with objects is that SQLite is orders of magnitude faster, which is largely due to how relational databases and object oriented programming languages fundamentally differ.

 SQLite is a powerful lightweight C library that is embedded in an iOS application. Using SQLite C library for local data storage implementation in iOS applications, one needs to be very meticulous when passing in strings and arguments required for the functions. SQLite is very particular about the argument type given to functions.

 A relational database is a type of database that organizes data into tables, and links them, based on defined relationships. These relationships enable you to retrieve and combine data from one or more tables with a single query.

Relational databases use the notion of databases separated into tables where each column represents a field and each row represents a record. Tables can be related or linked with each other with the use of foreign keys or common columns. Relational databases use the notion of databases separated into tables where each column represents a field and each row represents a record. Tables can be related or linked with each other with the use of foreign keys or common columns.

The design of a database schema can be visualised using following diagramdb1ACID properties:-

An important aspect of relational databases, which guarantees the reliability of transactions, is their adherence to the ACID properties: Atomicity, Consistency, Isolation and Durability. Each property is explained below in the context of databases.

Atomicity: Either all parts of a transaction must be completed or none.

Consistency: The integrity of the database is preserved by all transactions. The database is not left in an invalid state after a transaction.

Isolation: A transaction must be run isolated in order to guarantee that any inconsistency in the data involved does not affect other transactions.

Durability: The changes made by a completed transaction must be preserved or in other words be durable.

  • Non relational Databases (Core Data)

Core Data is the method recommended by Apple for local storage of app’s data. By default, core data uses SQLite as its main database in the iOS app. Internally Core Data make use of SQLite queries to save and store its data locally, which is why all the files are stored as .db files. This also eliminates the need to install a separate database. In iOS this framework allows for two different database storage types but by default it is SQLite.Core Data allows you deal with common functionalities of an app like, restore, store, undo and redo.

If you’ve worked with Object-relational mapping (O/RM) before: Core Data is not an O/RM. It’s much more. If you’ve been working with SQL wrappers before: Core Data is not an SQL wrapper. It does by default use SQL, but again, it’s a way higher level of abstraction. If you want an O/RM or SQL wrapper, Core Data is not for you.

One of the very powerful things that Core Data provides is its object graph management. This is one of the pieces of Core Data you need to understand and learn in order to bring the powers of Core Data into play.

Comparison between SQL and NoSQL(Core Data)

Sr.

SQL

NoSQL(Core Data)

1

Called as Relational Databases (RDBMS Table based) Called as non-relational (Object graph management)

2

Offers data constraints like “unique” keys Leaves data constraints to the business logic side of the program

3

Primary function is storing and fetching data Primary function is graph management (although reading and writing to disk is an important supporting feature)

4

Operates on data stored on disk (or minimally and incrementally loaded) Operates on objects stored in memory (although they can be lazily loaded from disk)

5.

Stores “dumb” data Works with fully-fledged objects that self-manage a lot of their behavior and can be sub classed and customized for further behaviors

6.

Can be transactional, thread-safe, multi-user Non-transactional, single threaded, single user (unless you create an entire abstraction around Core Data which provides these things)

7

Can drop tables and edit data without loading into memory Only operates in memory

8

Perpetually saved to disk (and often crash resilient) Requires a save process

9

Perpetually saved to disk (and often crash resilient Can create millions of new objects in-memory very quickly (although saving these objects will be slow)

 

  • Design requirements:- 

SQLite

1) Design Tools:-We can design SQLite database schema using SQLite Manager, SQLite Database Browser and Firefox SQLite extension.

2) Data Types Selection Criteria:-SQLite supports Text, Integer, BLOB and Real (Float, Double).

SQLite is “Type less ”, We can store any kind of data you want in any column of any table, regardless of the declared data type of that column. A data type to SQLite is any sequence of zero or more names optionally followed by parenthesized lists of one or two signed integers. Notice in particular that a data type may be zero or more names. That means that an empty string is a valid data type as far as SQLite is concerned.

Integer Primary Key: –

In SQLite table for storing unique (Primary Key) data always use INTEGER PRIMARY KEY columns must contain a 32-bit signed integer.

Comparison and Sort Order: –

For sorting and comparison always prefer to use numeric and text data type.

Blob: –

For storing Image or binary data always prefer to use Blob as a data type. SQLite provide functions to read and write the binary values.

Boolean: –

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

3) Normalization: –

Database normalization (or normalization) is the process of organizing the columns (attributes) and tables (relations) of a relational database to minimize data redundancy.

Normalization involves decomposing a table into less redundant (and smaller) tables without losing information; defining foreign keys in the old table referencing the primary keys of the new ones. The objective is to isolate data so that additions, deletions, and modifications of an attribute can be made in just one table and then propagated through the rest of the database using the defined foreign keys.

4) Conventions: –

  • When having logical noun/verb language separations, component names will be separated by the underscore character (‘_’) to indicate the separation (date_of_birth).
  • Table names will be defined in the plural. This is because a table is itself a collection of 1 or more row entities (plural), and this naming convention .Is also better understood in the object-oriented world (customer_addresses).
  • Table whose primary purpose are for defining Many to Many relationships must conform to the naming convention like (artist_song_maps_pk, artist_song_maps_artist_id_fk)
  • Constraint names must be suffixed by character codes to indicate the type of constraint it is. The character codes are defined in the following table:

If the constraint is a:

it’s name must be suffixed with:

primary key

_pk

foreign key

_fk

check

_ck

not null

_nn

unique

_uq

index

_idx

 

Core Data

 Design Tools:-

We can design Core data schema using XCode Data model Editor or Core Data Editor.

 

Data Types Selection Criteria

Core Data supports different data types like Transformable, String, Integer, Binary Data, Float, Double and Date.

Integer 16, Integer 32, and Integer 64 data types are for storing signed integers.

Decimal, Double, and Float data types are for storing fractional numbers

String data type is used for storing text contents.

Boolean data type is used for storing YES or NO values.

Date data type is used for storing dates as well as timestamps.

Binary data type is used for storing binary data.

Transformable: This data type can be used to store an instance of UIColor, UIImage, and so on. It archives objects to instances of NSData.

Conventions : – Naming Conventions are similar to SQLite