Search code examples
ioscore-datalockingwarningsunlock

'lock' is deprecated: first deprecated in iOS 8.0 - Use -performBlockAndWait: instead - iOS coredata warning


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


Solution

  • 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.