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];
});
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With