iOS 在 dispatch_async 中更新 RLMObject
<p><p>我想更新 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 中的 RLMObject 并在 dispatch_get_main_queue() 中获取结果,但是在其他线程中更新的对象在主 ui 线程中没有更新。什么是解决方案?示例代码为:</p>
<p><strong>结果是:狗的年龄1:9狗的年龄2:9</strong></p>
<p><strong>但应该是:狗的年龄1:9狗的年龄2:11</strong></p>
<pre><code>// Create a standalone object
Dog *mydog = [ init];
// Set & read properties
mydog.name = @"Rex2";
mydog.age = 9;
NSLog(@"Name of dog: %@", mydog.name);
// Realms are used to group data together
RLMRealm *realm = ; // Create realm pointing to default file
// Save your object
;
;
;
// Multi-threading
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *otherRealm = ;
RLMResults *otherResults = ;
Dog* dog = ;
NSLog(@"Age of dogs1: %ld", (long)dog.age);
;
dog.age = 11;
;
dispatch_async(dispatch_get_main_queue(), ^{
RLMRealm *otherRealm2 = ;
RLMResults *otherResults2 = ;
Dog* dog2 = ;
NSLog(@"Age of dogs2: %ld", (long)dog2.age);
});
});
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果您在各自的调度 block 顶部调用 <code></code> 和 <code></code>,这将确保给定 Realm 正在查看最多 -数据库中的最新事务。</p></p>
<p style="font-size: 20px;">关于iOS 在 dispatch_async 中更新 RLMObject,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31703603/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31703603/
</a>
</p>
页:
[1]