2016-06-08 40 views

回答

1

塊在與同時調用write()方法的線程相同的線程上執行。換句話說,如果在主線程上調用write(),則該塊將在主線程上執行。

dispatch_async(dispatch_queue_create("background", nil)) { 

    // Some operations in a background thread ... 

    try! realm.write { 
    // this block will be executed on the background thread 
    } 
} 

如果您想在主線程上執行寫操作,您可能需要根據需要分派到主線程。

dispatch_async(dispatch_queue_create("background", nil)) { 

    // Some operations in a background thread ... 

    dispatch_async(dispatch_get_main_queue()) { 
    try! realm.write { 
     // this block will be executed on the main thread 
    } 
    } 
} 
相關問題