3

I am trying to build an iOS swift application that uses SQLITE and syncs it to my PHP backend server .

I can build API that reads and write data from server . I can read and write data with swift from server making HTTP requests .

What I am trying to accomplish here is : saving data to local database and sync it to the server database , the local database is user specific and the server database contains all users data .

Local DB

--------------------------
| maint item | due date | 
| abc        | 29-3-2018
| DNA        | 24-1-2017
| boy        | 17-2-2017
--------------------------

Server DB

---------------------------
| Maine item | due date | user 
| abc        | 29-3-2018| Jane 
| DNA        | 24-1-2017| Jane
| boy        | 17-2-2017| Jane
| amc        | 22-7-2017| cameleon

How can I achieve that in swift and if possible please post example project .

2
  • Will you send those rows of data as array of items in JSON format? Commented Apr 21, 2017 at 18:38
  • I have no reason for doing or not doing that . Commented Apr 21, 2017 at 20:09

3 Answers 3

1

There are a lot of scenarios, but I recommend this one:

Separate the layers of the project I recommend VIPER, Clean swift or ReSwift architecture.

After that your best friend will be the Interactor ( Thunk in ReSwift), there you will navigate the API and Database managers.

example :

If I have internet, download info from the API check If there is a similar object in the local database and present the new one. If the local is new update the API (or not .. :) ).

If I don't have internet show the local and update the date if the local one is changed...

Of course, there is no simple solution something more about the subject https://en.wikipedia.org/wiki/Eventual_consistency

Sign up to request clarification or add additional context in comments.

1 Comment

I was asking more of like a standard / simple way to do it . Like I thought it would have been done several times before and there's some sort of a standards example by the way I don't require SQLLite
0

You could use FMDB to handle SQLite, just add it to your podfile:

pod "FMDB"

then install:

pod install

and import it in your bridging header:

#import <FMDB/FMDB.h>

The documentation is in Objective-C, but it's quite easy to translate that in swift. For example:

import FMDB

let path = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0] + "/database.db"
let db = FMDatabase(path: path)
guard db.open() else {
            log.error("Unable to open database")
            return
}
db.executeStatements("CREATE TABLE ...")

Regarding the syncing process it's not so straightforward because you have to handle conflicts, unique IDs and so on.

If SQLite is not a requirement take a look at CouchDB or SyncDB.

I hope this will help you!

2 Comments

Import the bridging header is not required if you use_frameworks! in your podfile. Then just import FMDB.
Actually SQLITE is not a requirement I'm looking at your suggestions .
0

You can look at http://github.com/groue/GRDB.swift.

It is a Swift SQLite library that ships with sample code for synchronizing a JSON document and a database table: https://github.com/groue/GRDB.swift/blob/master/Playgrounds/JSONSynchronization.playground/Contents.swift

The output of this demo playground is below. You can see that its algorithm is very efficient, and only applies the minimal amount of changes to the database:

-- Initial import {
--   "persons": [{
--     "id": 1,
--     "name": "Arthur"
--   }, {
--     "id": 2,
--     "name": "Barbara"
--   }, {
--     "id": 3,
--     "name": "Craig"
--   }, ]
-- }
SELECT * FROM persons ORDER BY id
INSERT INTO "persons" ("id", "name") VALUES (1,'Arthur')
INSERT INTO "persons" ("id", "name") VALUES (2,'Barbara')
INSERT INTO "persons" ("id", "name") VALUES (3,'Craig')

-- Import {
--   "persons": [{
--     "id": 2,
--     "name": "Barbie" (name is modified)
--   }, {
--     "id": 3,
--     "name": "Craig"  (not modified)
--   }, {
--     "id": 4,
--     "name": "Daniel" (new person)
--   }, ]
-- }    
SELECT * FROM persons ORDER BY id
DELETE FROM "persons" WHERE "id"=1
UPDATE "persons" SET "name"='Barbie' WHERE "id"=2
INSERT INTO "persons" ("id", "name") VALUES (4,'Daniel')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.