ios - 境界- cocoa : How to add notification inside other notification block
<p><p>在我的应用中,我注册了一个通知 block ,它本身必须注册另一个通知 block 。</p>
<pre><code>notificationToken = [self.appState.currentProject addNotificationBlock:^(BOOL deleted, NSArray<RLMPropertyChange *> *changes, NSError *error) {
if (deleted) {
NSLog(@"The object was deleted.");
} else if (error) {
NSLog(@"An error occurred: %@", error);
} else {
bool filterChanged = false;
for (RLMPropertyChange * change in changes){
if()
filterChanged = true;
}
if(filterChanged){
// compute predicate from filters
// predicate = [NSPredicate ...
styles =
notificationTokenStyles = [styles addNotificationBlock:^(RLMResults<Style *> * _Nullable results, RLMCollectionChange * _Nullable change, NSError * _Nullable error) {
// react on changed styles.
}]; // <- RLMException here
}
}];
}
}];
</code></pre>
<p>我知道这在技术上是不可能的,但对我来说这是必要的。我可以以某种方式将嵌套的 addNotificationBlock 分派(dispatch)或推迟到另一个线程左右吗?我知道我可以等待 500 毫秒,但这会导致竞争条件。</p>
<p>现在,我的问题是,当我执行导致第一个通知 block 执行的写入事务时,我遇到 <code>RLMException: Cannot register notification blocks from within write transactions.</code> 就在嵌套的 addNotificationBlock。</p>
<p>提前致谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是一个已知问题(参见 <a href="https://github.com/realm/realm-cocoa/issues/4818" rel="noreferrer noopener nofollow">this Realm issue on GitHub</a>)。</p>
<p>您可以像这样(Swift 代码)包装新 <code>NotificationToken</code> 的创建:</p>
<pre><code>DispatchQueue.main.async {
// register observation blocks here
}
</code></pre>
<p>或者你可以在开始写事务之前调用<code>realm.refresh()</code>。</p></p>
<p style="font-size: 20px;">关于ios - 境界- cocoa : How to add notification inside other notification block,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47339378/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47339378/
</a>
</p>
页:
[1]