Preferred approach for background syncing while allowing user read/write access in an iOS app.

65 views
Skip to first unread message

Jeff Bailey

unread,
Feb 16, 2017, 5:43:01 PM2/16/17
to FMDB
I've spent the past few hours searching this group and other places to try to find the best approach for supporting background syncing while allowing the user to continue using the application.  User access is mostly read-only, but they can add/edit some content.  This is currently supported using a FMDatabaseQueue and works great.  Now, I'm starting to work on the background sync:

- Periodically, a background thread will request database updates from the server by calling a rest service
- A JSON payload will be returned with adds/updates/deletes for each table
- The background thread will process this JSON and update the local sqlite database

I attempted to create a separate instance of FMDatabaseQueue for the background sync, but I'm getting sporadic "database is locked" errors.

What's the best approach?

Thanks!
Jeff

Holly Schilling

unread,
Feb 16, 2017, 5:49:32 PM2/16/17
to fm...@googlegroups.com
If you're asking this question, you're probably engineering it wrong. You should be able to safely use the same instance of FMDatabaseQueue in both the foreground and background. The catch is to ensure you are using it for small, atomic transactions. Processing should not be done within the work queue and individual transactions should be as small as possible.

If it is not possible to sync with small, atomic transactions, you should create a temporary database in a new file and then merge the data into the primary database when the final result is complete, or just swap files.

Best luck,

Holly 


--
You received this message because you are subscribed to the Google Groups "FMDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fmdb+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Bailey

unread,
Mar 16, 2017, 9:02:49 AM3/16/17
to FMDB
To close the loop on this in case it helps others in the future.

I ended up switching from FMDB to GRDB since it directly supports multiple readers/single writer using its DatabasePool class. It's written in Swift and was obviously inspired by FMDB.  The conversion was relatively straightforward.  
Reply all
Reply to author
Forward
0 new messages