簡體   English   中英

如何同時從兩個不同的控制器管理兩個AFNetworking操作

[英]how to manage concurrently two AFNetworking operations from two different controllers

我正在從兩個視圖控制器運行兩個AFJSONRequestOperation ,它們在另一個之后立即被調用:

viewcontroller1:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){

//
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response,NSError *error, id JSON){
//
}];
[operation start];
[SVProgressHUD showWithStatus:@"Searching for products, please wait.."];

viewcontroller2:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){

//
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response,NSError *error, id JSON){
//
}];
[operation start];
[SVProgressHUD showWithStatus:@"Loading categories.."];

問題在於,顯示的狀態僅適用於第二項操作: Loading categories..盡管它們完成了聯網並獲得JSON結果都很好。

似乎我不能使用NSOperationQueue因為這兩個操作位於單獨的控制器中。 那么如何確保等到第一個完成后再向用戶顯示兩個狀態消息呢?

一些想法:

  1. 如果確實需要,您當然可以對兩個操作使用相同的操作隊列。 您可以將操作隊列從第一個控制器傳遞到第二個控制器。 或者您可以為隊列設置一個單例類。

  2. 不過,這是有爭議的,因為您還可以在完成第一個操作時就建立第二個操作的依賴關系,而無需使用共享隊列。 您只需NSOperation第一個請求的NSOperation傳遞給第二個控制器,然后將第一個操作設置為第二個控制器的dependency

  3. 不過,這也是有爭議的,因為您為什么要在不需要的情況下引入人為的串行操作約束。 如果同時運行而不是順序運行,則兩個網絡操作的運行速度將大大提高。 為了簡化更新HUD的工作,以較慢的性能懲罰用戶似乎很可恥。

  4. 我認為正確的解決方案是支持並發操作並適當更新HUD。 也許維護一個可變的狀態消息數組,您可以在列表中添加和刪除它們。

也許像這樣:

@interface HUDStatus ()
@property (nonatomic, strong) NSMutableArray *statusMessages;
@end

@implementation HUDStatus

+ (instancetype)sharedHudManager
{
    static id sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        _statusMessages = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addMessage:(NSString *)message
{
    [self.statusMessages addObject:message];
    [SVProgressHUD showWithStatus:message];
}

- (void)removeMessage:(NSString *)message
{
    __block BOOL didFindMessage = NO;

    // remove the message (not sure which one it is, so we'll look through all of them

    [self.statusMessages enumerateObjectsWithOptions:0 usingBlock:^(NSString *oldMessage, NSUInteger idx, BOOL *stop) {
        if ([oldMessage isEqualToString:message])
        {
            [self.statusMessages removeObjectAtIndex:idx];
            didFindMessage = YES;
            *stop = YES;
        }
    }];

    if (!didFindMessage)
        NSLog(@"%s: Trying to remove '%@' but it was not found in %@", __FUNCTION__, message, self.statusMessages);

    // if, having removed
    if ([self.statusMessages count] > 0)
        [SVProgressHUD showWithStatus:[self.statusMessages lastObject]];
    else
        [SVProgressHUD dismiss];   // I don't know this class, so I don't know what the name of the method to dismiss the HUD
}

@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM