steipete / Aspects

Delightful, simple library for aspect oriented programming in Objective-C and Swift.
https://twitter.com/steipete
MIT License
8.4k stars 1.26k forks source link

'OSSpinLock' is deprecated: first deprecated in iOS 10.0 - Use os_unfair_lock() from <os/lock.h> instead #128

Open JunyuKuang opened 7 years ago

JunyuKuang commented 7 years ago

Hi, after I update my app's minimum iOS version to 10.0, I get this warning: 'OSSpinLock' is deprecated: first deprecated in iOS 10.0 - Use os_unfair_lock() from <os/lock.h> instead

I tried to fix it by myself but found it more complicate than I expected.

smitsuchak commented 7 years ago

I have added these changes and its working. UIKit added just because I had to keep the backward compatibility.

#import <os/lock.h>
#import <UIKit/UIKit.h>
static void aspect_performLocked(dispatch_block_t block) {
-    static OSSpinLock aspect_lock = OS_SPINLOCK_INIT;
-    OSSpinLockLock(&aspect_lock);
-    block();
-    OSSpinLockUnlock(&aspect_lock);
+    
+    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
+    if(([[[UIDevice currentDevice] systemVersion] compare:@"10.0" options:NSNumericSearch] != NSOrderedAscending)){
+    static os_unfair_lock aspect_lock = OS_UNFAIR_LOCK_INIT;
+        os_unfair_lock_lock(&aspect_lock);
+        block();
+        os_unfair_lock_unlock(&aspect_lock);
+    }
+    #else
+    else{
+        static OSSpinLock aspect_lock = OS_SPINLOCK_INIT;
+        OSSpinLockLock(&aspect_lock);
+        block();
+        OSSpinLockUnlock(&aspect_lock);
+    }
+    #endif
 }
steipete commented 7 years ago

Is that worth it? It's not like spin locks no longer work. Aspects evolved internally at PSPDFKit a bit but we use our own wrapper for locking. I will clean that up once we internally drop iOS 9... easier.