- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段代码,我想做的是让 self 在将在主线程上执行的 block 中保持事件状态。结果有点随机,有时会打印 null。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.proArray = [[NSMutableArray alloc]init];
GCDVC2* __weak weakSelf = self;
self.postGCDBlock = ^{
GCDVC2* __strong strongSelf2 = weakSelf;
[strongSelf2.proArray removeObject:@"3"];
NSLog(@"%@",strongSelf2.proArray);
[strongSelf2.activityIndicator stopAnimating];
};
self.addObjectsBlock = ^{
GCDVC2* __strong strongSelf = weakSelf;
[strongSelf.proArray addObject:@"2"];
[strongSelf.proArray addObject:@"3"];
[NSThread sleepForTimeInterval:5];
dispatch_async(dispatch_get_main_queue(),strongSelf.postGCDBlock);
};
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), self.addObjectsBlock);
}
这段代码工作正常:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.proArray = [[NSMutableArray alloc]init];
GCDVC2* __weak weakSelf = self;
//self.postGCDBlock = ;
self.addObjectsBlock = ^{
GCDVC2* __strong strongSelf = weakSelf;
[strongSelf.proArray addObject:@"2"];
[strongSelf.proArray addObject:@"3"];
[NSThread sleepForTimeInterval:5];
GCDVC2* __weak weakSelf2 = strongSelf;
dispatch_async(dispatch_get_main_queue(),^{
GCDVC2* __strong strongSelf = weakSelf2;
[strongSelf.proArray removeObject:@"3"];
NSLog(@"%@",strongSelf.proArray);
[strongSelf.activityIndicator stopAnimating];
});
};
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), self.addObjectsBlock);
}
有什么方法可以将第二段代码转换为与第一段代码的结构一起工作?我尝试了很多变化,但它总是随机的。我能以某种方式确保 self.postGCDBlock 不会将 self 设置为 nil 吗?
更新:属性(property)声明:
typedef void(^CustomBlock)(void);
@interface GCDVC2 ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property(nonatomic,strong)NSMutableArray *proArray;
@property (nonatomic, copy) CustomBlock addObjectsBlock;
@property (nonatomic, copy) CustomBlock postGCDBlock;
@end
最佳答案
我认为你的问题在于这一行(我无法用这段代码重现失败案例):
dispatch_async(dispatch_get_main_queue(),strongSelf.postGCDBlock);
此时,在您的 addObjectsBlock
中,strongSelf
持有对 self
的引用,但当您离开该 block 的范围时,它会结束. dispatch_async
将复制 postGCDBlock
,但该 block 没有对 self
的强引用。
要让 dispatch_async
持有对 self
的强引用,您需要执行如下操作:
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.postGCDBlock();
});
在 block 中包装 strongSelf
将导致 dispatch_async
保留 strongSelf
(从而 self
)足够长的时间它调用 postGCDBlock
。
关于iOS - GCD 和 __strong 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664790/
对 Objective-C 很陌生,无法弄清楚我错过了什么。 .h文件 #import #import "RESTResponse.h" typedef void (^Callback)(RESTR
从 xcode 5.0 移动到 5.1 时,GPUImage 库出现以下错误。在谷歌上搜索后,我发现我需要像这样发送 int [NSNumber numberWithInt:number] 但问题是我
有一个方法: - (void)doSmth:(NSString *__strong*)str { NSLog(@"%@", *str); } 当 __strong 跟在方法参数的类之后是什么意
我看到有人用下面的 __strong static foo *_foo = nil; 我想知道这里是否需要__strong;我的意思是 __strong 是默认值?是吗? 最佳答案 因为强引用是 de
我有这段代码,我想做的是让 self 在将在主线程上执行的 block 中保持事件状态。结果有点随机,有时会打印 null。 - (void)viewDidLoad { [super view
该项目未启用 ARC,但我们(错误地)使用符合 ARC 的代码库 - 特别是一个用于创建 GCDSingleton.h 中定义的单例对象的代码库: #define DEFINE_SHARED_INST
当我做类似下面的事情时,我得到一个错误提示 for (UIView* att in bottomAttachments) { if (i <= [cells count]) {
所以说我对核心数据很菜鸟。我有两个 NSManagedObjects,Map 和 AddressAnnotation。在我的一个 ViewController 中,我这样做: Map *aMap =
这就是返回错误的原因: NSMutableArray *newArray = [[[NSMutableArray alloc] initWithArray:[NSJSONSerialization J
在 Objective-C 中,我们将值转换为 __weak 以避免在某些情况下发生循环引用。 This post解释了为什么 __strong 很有用 __weak typeof (self) we
我检查了几个建议的链接,但没有找到答案。我想将两个浮点值传递给对象 breuk 的初始化。该对象有一个初始化方法initwithTeller: (float) mijnTeller 和Noemer:
我在这里阅读了有关 __strong 引用和 __weak 引用的用法:Explanation of strong and weak storage in iOS5 我试着写了一些代码来展示这些知识。
在 objective-C 中,为什么我们不能 alloc+init 或 new 具有父类(super class)的基类对象,而我们可以使用父类(super class)的构造函数来初始化吗? 下面
考虑这段代码: @implementation MyClass -(void)dealloc { NSLog(@"MyClass dealloc: %@", self); } @end @im
当我的 Xcode 项目使用 .mm 文件编译时出现多个错误并且所有错误都是相同的=无。当我将 C++ 代码放入 .mm 时出现错误时,还包含一些 .cpp 文件和 .mm 没有出现问题。我们都知道如
有圆弧 测试1: @interface test01ViewController () @property (strong) void(^myBlock)(id obj, NSUInteger idx
我正在使用以下方式对我的 NSSet 进行排序: NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name
当我如下声明 appDelegate 接口(interface)以设置 NSXMLParserDelegate 时,我从其他使用 [[UIApplication sharedApplication]
在阅读 Foundation 的标题时,我发现了这一点: - (__strong const char *)UTF8String NS_RETURNS_INNER_POINTER; // Conv
在写我最初的问题时,我无意中解决了我的问题。我想了解为什么我的更改会导致修复。 我写了一个接受 NSString 的类方法和 NSError ** .该方法返回YES如果该字符串是我的应用程序中的有效
我是一名优秀的程序员,十分优秀!