Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone IOS中的GCD和代码重用性?_Iphone_Objective C_Grand Central Dispatch - Fatal编程技术网

Iphone IOS中的GCD和代码重用性?

Iphone IOS中的GCD和代码重用性?,iphone,objective-c,grand-central-dispatch,Iphone,Objective C,Grand Central Dispatch,我有一个名为dataLoadingForChallenges的方法。在这种情况下,我会在需要时调用另外两个方法。我在我的类中使用这个方法两到三次来处理不同的事件,有一次我想在CellForRowatineXpath方法中异步调用这个方法。以下代码是否可接受。我不想将dataLoadingForChallenges中的所有代码粘贴到这个GCD块中。从GCD块调用可重用方法的方法是什么 dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_Q

我有一个名为dataLoadingForChallenges的方法。在这种情况下,我会在需要时调用另外两个方法。我在我的类中使用这个方法两到三次来处理不同的事件,有一次我想在CellForRowatineXpath方法中异步调用这个方法。以下代码是否可接受。我不想将dataLoadingForChallenges中的所有代码粘贴到这个GCD块中。从GCD块调用可重用方法的方法是什么

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(q, ^{
            [self dataLoadingForChallenges];
                 });

您可以将一个方法调用,甚至两个更大的once封装在您拥有的块中。然后,您的调度块会稍微干净一些,并且该块可以在任何时候被触发:

typedef void (^DataLoadBlock)();

@interface MyClass ()

@property (nonatomic, copy) DataLoadBlock dataLoadBlock;

@end

- (id)init {
    self = [super init];

    //Avoid a retain cycle with blocks that we own referencing self
    __weak id weakSelf = self;
    _dataLoadBlock = ^{
        id blockSelf = weakSelf;
        [blockSelf dataLoadingForChallenges];
        //Or put your other two messages, or even the bodies of those methods in the block.
    };
    return self;
}

- (void)loadData {
    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(q, self.dataLoadBlock);
}

您好@CodaFi,请您提供更多信息。何时调用此init方法。另外,请提供任何可能对我有帮助的链接/教程。谢谢你reply@RamuPasupuleti这是伪代码。老实说,你不能指望我将堆栈溢出答案与你的代码库完美地集成在一起。另外,关于你的问题,我已经尽我所能解释了。如果你想要一个教程,问一个不同的问题。可能有人想要钓鱼而不是钓鱼课。