If you have multiple threads updating a database, you can create a dedicated serial queue for your SQLite interaction and dispatch all database interaction to that single queue. That way, even though you have multiple threads performing network operations, you can dispatch all database updates to this new dedicated queue, and it will completely eliminate all database contention issues. You're probably having database updates fail because you have multiple threads trying to update the same database and some of them are likely failing.
The fact that you suspect might have database updates silently failing is worrying. Are you possibly not checking the return codes of all of your SQLite calls? It really is critical to check the return code every SQLite call, and if it failed, look at sqlite3_errmsg
(or if using FMDB, lastErrorMessage
). If you don't do this, you're just flying blind. You're fortunate at this point that the problem was obvious, but next time the problem may be more subtle, and you'll pull your hair tracking down the issue.
Finally, since you're already using AFNetworking, I'd also suggest that you consider using AFHTTPRequestManager
. Specifically, rather than building the URL yourself, use the GET
method, passing the params
dictionary. Your stringByAddingPercentEncodingWithAllowedCharacters
code will generally work, but may fail in certain scenarios (notably, the unlikely scenario that your parameter value contained a +
or &
in it). Furthermore, if you use GET
and set the request manager's operationQueue.maxConcurrentOperationCount
to something reasonable, e.g. 4, you'll also eliminate the possibility of requests timing out unnecessarily on slow connections. Bottom line, there are a couple of subtle networking issues that AFHTTPRequestManager
will handle, but your current implementation won't.