Toydor
Toydor

Reputation: 2327

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];
});

Upvotes: 7

Views: 2242

Answers (2)

seb
seb

Reputation: 2385

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.

Upvotes: 9

aman.sood
aman.sood

Reputation: 874

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

Upvotes: 1

Related Questions