我已经过度使用了一个请求在线服务获取结果的getter.如何强制getter仅从同步块返回结果?
@interface MyClass ()
@property (nonatomic, strong) NSMutableDictionary* myDictionary;
@end
@implementation MyClass
-(NSMutableDictionary*) myDictionary {
dispatch_async(queue, ^{
/* perform online request */
dispatch_sync(dispatch_get_main_queue(), ^{
// I need to obtain lock until this line gets executed and only then return
});
});
}
@end
Run Code Online (Sandbox Code Playgroud)
有一个非常好的谷歌搜索至少3小时我遇到了dispatch_group_async,dispatch_semaphore和__block.我不知道我是否错误地使用它们但是没有达到目的.
更新1:
myDictionary是一个异步属性.我想看看这是否可以通过getter本身来实现.
我希望在用户单击“注销”时销毁会话,我试图通过使用 jQuery 调用 onClick 函数来执行此操作,然后该函数将调用包含 .php 的 .PHP 文件session_destroy();。
索引.php
<?php
if(isset($_SESSION['username'])) {
echo "<li><a href='#' id='logOut' onclick='logOut();'>Log Out</a></li>";
}
?>
Run Code Online (Sandbox Code Playgroud)
这使得注销按钮仅在用户登录并且 onClick 应该调用logOut()Nav.js 中包含的时候可见。
导航.js
function logOut() {
$.get("../Controller/logOut.php");
return false;
}
Run Code Online (Sandbox Code Playgroud)
登出.php
<?php
session_destroy();
?>
Run Code Online (Sandbox Code Playgroud)
当我单击“注销”时,似乎没有任何反应,我在 Chrome Dev 控制台中也没有收到任何错误。问题是什么?