Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSKeyedArchiver thread safe?

Do I need to worry about using archiveRootObject and unarchiveObjectWithFile in background threads?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [NSKeyedArchiver archiveRootObject:myArray toFile:file];
});
like image 704
Toydor Avatar asked Jul 13 '15 10:07

Toydor


Video Answer


2 Answers

You should be very careful with that. archiveRootObject is not an atomic operation. For instance, if you call archiveRootObject on a background thread while another thread changes the state of the object being archived, you could end up archiving an illegal state of that object. Of course this heavily depends on your application and how you implemented archive/unarchive routines for your objects.

However, if you can make sure that the object doesn't change his state while the archiving background thread is in progress, it should be safe to do so.

like image 164
seb Avatar answered Sep 18 '22 23:09

seb


As its a synchronous call, so if you are saving heavy data and don't want to block UI then you should consider background thread.

Else if your requirement is to show some status/ check/ operation on completion then you don't need a thread.

Finally it all depends on your app requirement.

Check more at link

like image 29
aman.sood Avatar answered Sep 18 '22 23:09

aman.sood