sqlite and threading with iPhone SDK

后端 未结 4 541
粉色の甜心
粉色の甜心 2020-12-10 21:37

I have an iPhone app that is using sqlite 3.6 (not with FMDB) to store and load data. I load the database when the app loads and uses the same database connection through th

4条回答
  •  旧巷少年郎
    2020-12-10 22:18

    I solved this problem by using one thread and an NSOperationQueue to insert the Data. I would give it some thought. I've never been able to get a stable System with mutliple threads, and most writes aren't that important that queuing really helps.

    As per request, some more Infos:

    I have a subclass of NSOperation that I instantiate with the model object I want to store. These operations are than submitted to an extension of NSOperationsQueue that runs in a seperate thread. This custom Queue just adds a pointer to the database instance. When the operation is executed, it uses the [NSOperationsQueue currentQueue] property to access the queue and than the database. On purpose, i used non-concurrent operations (maxOperations was set to 1)
    Hence, only one query (or update) is executed at a time consecutivly, completely in the background.

    Obviously you need some kind of callback after you're finished.

    It is possibly not the fast, but the most stable and cleanest solution i could find.

    Docs:
    http://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationObjects/OperationObjects.html
    http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/
    http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/

提交回复
热议问题