当我通过GCD多线程掌握我的技能时,我遇到了一些问题.假设您有以下方法:
- (void)method {
NSString *string= [NSString string]; //will be autoreleased
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//very very lengthy operation...
NSLog(@"%@", string); //is it safe?
});
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是否正确,因为我认为我应该在块执行之前保留字符串:实际上我担心事件循环结束并在块中string使用之前发送自动释放消息string.这会使程序崩溃.
我对吗?我应该发送保留和发布消息,string还是这是正确的实现?提前致谢!
cocoa multithreading memory-management objective-c grand-central-dispatch
在我的应用程序中,我有表视图控制器.当用户在tableview的最后一行键入时,应显示操作表以要求注销.以下是此操作的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
//..
case 1:
//..
case 2:
//..
case 3:
let logOutMenu = UIAlertController(title: nil, message: "Are you sure want to logout?", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let logOutAction = UIAlertAction(title: "Log out", style: .default, handler: { (UIAlertAction) in
print("sign out")
})
logOutMenu.addAction(cancelAction)
logOutMenu.addAction(logOutAction)
self.present(logOutMenu, animated: true, completion: nil)
default: break
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但行动表有奇怪的行为.显示操作表大约需要10秒钟(甚至更长时间).我在真实设备上也注意到了同样的行为.我做错了什么?