正如@maddy 所提到的,您将希望为您的 getPrices 方法使用完成块而不是返回——返回 + 异步不要混合。
这将是将您的 getPrices 方法转换为的一般形式:
- (void)_getPricesWithCompletion:(void(^)(NSMutableArray *sectionPriceArray))priceCompletion;
这个站点:http://goshdarnblocksyntax.com 有一些常见的块语法声明用法。
通常您会调用此异步方法,然后在完成块中设置您的 iVar,并在接收到新数据后重新加载关联的 UI 元素。大致如下:
[self _getPricesWithCompletion:^(NSMutableArray *sectionPriceArray) {
self.sectionPriceArray = sectionPriceArray;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// reload any UI elements dependent on your sectionPriceArray here
}];
}];
现在在您展示的示例代码中,您似乎正在使用 NSOperationQueue 来排队不同的操作。这里的事情可能会变得更复杂一些。后续队列操作不会等待您的异步操作在执行前完成。因此,例如,如果您在 getPrices 操作之后有一个利用价格获取结果的操作,那么 iVar 几乎肯定不会在该点包含正确的数据。在这种情况下,您需要使用某种信号量来处理等待异步操作完成,然后再继续执行依赖于它的操作。
这是我的意思的一个例子:
NotProperlyWaiting.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NotProperlyWaiting : NSObject
@property (strong, nullable) NSMutableArray *sectionPriceArray;
- (void)callOperations;
- (void)fakeServerCallWithCompletion:(void(^)(NSData *tw_result, NSURLResponse *response, NSError *error))completion;
@end
NotProperlyWaiting.m
#import "NotProperlyWaiting.h"
@interface NotProperlyWaiting()
- (void)_getPricesWithCompletion:(void(^)(NSMutableArray *sectionPriceArray))priceCompletion;
- (void)_printPricesArray;
@end
@implementation NotProperlyWaiting
- (instancetype)init {
self = [super init];
if (self) {
_sectionPriceArray = [NSMutableArray array];
}
return self;
}
- (void)callOperations {
// setup our completion block to be passed in (this is what will eventually set the self.sectionPricesArray
void (^pricesCompletion)(NSMutableArray *) = ^ void (NSMutableArray *sectionPricesArrayFromCompletion){
self.sectionPriceArray = sectionPricesArrayFromCompletion;
};
NSOperationQueue *myQueue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(_getPricesWithCompletion:) object:pricesCompletion];
NSInvocationOperation *printOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(_printPricesArray) object:nil];
[myQueue addOperation:operation];
[myQueue addOperation:printOperation];
}
- (void)_getPricesWithCompletion:(void(^)(NSMutableArray *sectionPricesArray))priceCompletion {
[self fakeServerCallWithCompletion:^(NSData *tw_result, NSURLResponse *response, NSError *error) {
// check the error or response or whatever else to verify that the data is legit from your server endpoint here
// then convert the data to your mutable array and pass it through to our completion block
NSString *stringData = [[NSString alloc] initWithData:tw_result encoding:NSUTF8StringEncoding];
NSMutableArray *tempPricesArray = [NSMutableArray arrayWithArray:[stringData componentsSeparatedByString:@"\n"]];
// now our completion block passing in the result prices array
priceCompletion(tempPricesArray);
}];
}
- (void)_printPricesArray {
NSLog(@"NotWaiting -- Prices array : %@", self.sectionPriceArray);
}
// this is a fake version of NSURLSession
- (void)fakeServerCallWithCompletion:(void(^)(NSData *tw_result, NSURLResponse *response, NSError *error))completion {
NSString *fakeServerResponse = @"FirstThing\nSecondThing\nThirdThing";
NSData *fakeData = [fakeServerResponse dataUsingEncoding:NSUTF8StringEncoding];
NSURLResponse *fakeResponse = [[NSURLResponse alloc] init];
NSError *fakeError = [NSError errorWithDomain:@"FakeErrorDomain" code:33 userInfo:nil];
// never call sleep in your own code, this is just to simulate the wait time for the server to return data
sleep(3);
completion(fakeData,fakeResponse,fakeError);
}
NS_ASSUME_NONNULL_END
ProperlyWaiting.h(NotProperlyWaiting.h 的子类重用 callOperation 和 fakeServerCallWithCompletion:)
#import "NotProperlyWaiting.h"
NS_ASSUME_NONNULL_BEGIN
@interface ProperlyWaiting : NotProperlyWaiting
@end
NS_ASSUME_NONNULL_END
ProperlyWaiting.m
#import "ProperlyWaiting.h"
@interface ProperlyWaiting()
- (void)_getPricesWithCompletion:(void(^)(NSMutableArray *sectionPricesArray))priceCompletion;
- (void)_printPricesArray;
@property dispatch_semaphore_t semaphore;
@end
@implementation ProperlyWaiting
- (void)callOperations {
self.semaphore = dispatch_semaphore_create(0);
[super callOperations];
}
// identical implementations to NotProperlyWaiting, but this time we'll use a semaphore to ensure the _printPricesArray waits for the async operation to complete before continuing
- (void)_getPricesWithCompletion:(void(^)(NSMutableArray *sectionPricesArray))priceCompletion {
[self fakeServerCallWithCompletion:^(NSData *tw_result, NSURLResponse *response, NSError *error) {
// check the error or response or whatever else to verify that the data is legit from your server endpoint here
// then convert the data to your mutable array and pass it through to our completion block
NSString *stringData = [[NSString alloc] initWithData:tw_result encoding:NSUTF8StringEncoding];
NSMutableArray *tempPricesArray = [NSMutableArray arrayWithArray:[stringData componentsSeparatedByString:@"\n"]];
// now our completion block passing in the result prices array
priceCompletion(tempPricesArray);
// signal our semaphore to let it know we're done
dispatch_semaphore_signal(self.semaphore);
}];
}
- (void)_printPricesArray {
// wait for the semaphore signal before continuing (so we know the async operation we're waiting on has completed)
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"Waiting -- Prices array : %@", self.sectionPriceArray);
}
@end
类的示例调用如下:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NotProperlyWaiting *notWaiting = [[NotProperlyWaiting alloc] init];
[notWaiting callOperations];
ProperlyWaiting *waiting = [[ProperlyWaiting alloc] init];
[waiting callOperations];
}
日志中的输出将是:
NotWaiting -- Prices array : (
)
然后 3 秒后:
Waiting -- Prices array : (
FirstThing,
SecondThing,
ThirdThing
)
一些附加链接指向与此主题相关的有用文档:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html
https://developer.apple.com/documentation/dispatch/1452955-dispatch_semaphore_create