3

I'm using Xcode 7.1 and I get this warning when opening my existing app. Would be helpful if someone shows the way to use -performBlockAndWait:

enter image description here

Thanks

a4arpan
  • 1,828
  • 2
  • 24
  • 41
Confused
  • 3,846
  • 7
  • 45
  • 72
  • You should not post these kinds of screenshots. Please post code as text, formatted as code. – Mundi Dec 17 '15 at 13:45
  • Thank you. I added this as screenshot as I want to show the warning message. Hereafter, I'll post as code. – Confused Dec 23 '15 at 23:56
  • You accepted the answer that was exactly mine which was posted before. You could at least up-vote my answer. – Mundi Dec 24 '15 at 09:50

2 Answers2

6

As Mundi said, you don't need a lock for what you are doing. However, to address your general question about lock and unlock being deprecated...

You should be using performBlock or performBlockAndWait instead. These methods are similar to the ones on NSManagedObjectContext.

So, instead of manually locking the critical region, you put that code into a block which gets "performed."

For example, if you have this code...

[persistentStoreCoordinator lock];
[persistentStoreCoordinator doSomeStuff];
[persistentStoreCoordinator unlock];

you would replace it with...

[persistentStoreCoordinator performBlock:^{
    [persistentStoreCoordinator doSomeStuff];
}];

Note that performBlock is an asynchronous operation, and it will return immediately, scheduling the block of code to execute on some other thread at some point in the future.

This should be OK, as we should be doing most of our programming with an asynchronous model anyway.

If you must have synchronous execution, you can use the alternative, which will complete execution of the block before returning to the calling thread.

[persistentStoreCoordinator performBlockAndWait:^{
    [persistentStoreCoordinator doSomeStuff];
}];

Again, these behave exactly like their NSManagedObjectContext counterparts.

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
0

Simply delete the offending lines. There is no need for any lock when adding a persistent store to the coordinator.

Also, you should be using the latest Xcode, currently 7.2.

Mundi
  • 79,884
  • 17
  • 117
  • 140