ios - MagicalRecord with NSOperation causing persistence issues -
i'm using magicalrecord manage core data. app receives chunks of data needs iterated on individually added store records, , saved. put code nsoperation. looks this:
class addincomingmessageoperation: nsoperation { override func main() { let context = nsmanagedobjectcontext.mr_context() self.message = message.createmessage( json: self.messagejson, context: context) if self.message != nil { context.mr_savetopersistentstoreandwait() } } }
running on nsoperationqueue.mainqueue
seems work without issue, other holding ui. next move run of these operations on own background operation queue. adding them nsoperationqueue , running them results in mixed data.
what mean - createmessage function checks existing conversation object, , adds conversation if exists, or creates 1 if doesn't. requires knowing exists in store. seems happening how, them running on background queue, they're creating conversations have been created in operation.
i can solve of setting operationqueue.maxconcurrentoperationcount = 1
, slows whole thing down.
you cannot use main thread context on background thread. not in core data , not in magical record.
use mr methods designed background operations, such savewithblock
. create background contexts mr_newcontext
.
if make use of these simple apis, might able dispense cumbersome nsoperation
subclasses.
Comments
Post a Comment