2014-11-20 40 views
0

我有三個API的我從我的ViewController.m內拉UITableView,並將其放入UITableView即使一個API失敗,加載視圖控制器

如果其中一個網站未加載,是否還有辦法讓UITableView加載?

現在,ViewController.m只是不加載,如果所有3個來源沒有按我的方法加載。

這是我使用的方法:在ViewController.mUITableView

- (void)loadOneWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success 
        failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure { 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *tNE = [defaults objectForKey:[NSString stringWithFormat:@"tNE%@", bn]]; 
    NSString *path = [NSString stringWithFormat:@"xx/%@/", tNE]; 

    [self.eObjectManager getObjectsAtPath:path parameters:nil success:success failure:failure]; 
} 


- (void)loadMedia { 
    self.combinedModel = [NSMutableArray array]; 
    // Here's the #1 
    [self loadOneWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

     [self.combinedModel addObjectsFromArray:mappingResult.array]; 

    // Here's the trick. call API2 here. Doing so will serialize these two requests 
    [self loadTwoWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

     [self.combinedModel addObjectsFromArray:mappingResult.array]; 

    // Here's the trick. call API3 here. Doing so will serialize these two requests 
    [self loadThreeWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

     [self.combinedModel addObjectsFromArray:mappingResult.array]; 

     [self sortCombinedModel]; 

     [self.tableView reloadData]; 

    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"No?: %@", error); 
    }]; 

    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
      NSLog(@"No?: %@", error); 
    }]; 

    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"No?: %@", error); 
    }]; 
} 

所以如果API1不加載,API2API3仍然會加載和顯示。

回答

1

的loadOne,loadTwo ...功能有一個缺點是,他們需要兩個塊參數,一個成功,一個失敗。如果您將這些更改爲採用處理成功或失敗的單個模塊,那麼發生錯誤後將更容易進行。

編輯通過不直接傳遞完成和失敗塊來改變如何調用eObjectManager。相反,實施這些塊和重新排列PARAMS相匹配的單塊接口...

- (void)betterLoadOneWithCompletion:(void (^)(RKObjectRequestOperation*, RKMappingResult*, NSError *))completion { 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *tNE = [defaults objectForKey:[NSString stringWithFormat:@"tNE%@", bn]]; 
    NSString *path = [NSString stringWithFormat:@"xx/%@/", tNE]; 

    [self.eObjectManager getObjectsAtPath:path parameters:nil success:^(RKObjectRequestOperation *op, RKMappingResult *map) { 
     // success! pass the operation, map result and no error 
     completion(op, map, nil); 
    } failure:^(RKObjectRequestOperation *op, NSError *error) { 
     // fail. pass the operation, no result and the error 
     completion(op, nil, error); 
    }]; 
} 

它仍然可以調用舊的功能或與兩個街區的一些外部庫,但它的結果組合成一個單獨的塊。這個調用者期望他們將得到一個好的RKMappingResult和一個nil NSError,或者一個無結果參數和一個錯誤實例。有了這個api,我們可以輕鬆修復您的方法,只需記錄錯誤發生時的情況,並繼續進行,錯誤或不...

- (void)loadMedia { 
    self.combinedModel = [NSMutableArray array]; 

    // changed the loadOneWithCompletion signature to take just a single block, calling it on success or fail 
    [self betterLoadOneWithCompletion:^(RKObjectRequestOperation *op, RKMappingResult *mappingResult, NSError *error) { 
     // if it worked, handle the results 
     if (!error) { 
      [self.combinedModel addObjectsFromArray:mappingResult.array]; 
     } else { 
      // if it didn't work, log the error, but execution continues 
      NSLog(@"No?: %@", error); 
     } 
     // even if it didn't work, we can keep going... 
     [self betterLoadOneWithCompletion:^(RKObjectRequestOperation *op, RKMappingResult *mappingResult, NSError *error) { 
      // same - handle results 
      if (!error) { 
       [self.combinedModel addObjectsFromArray:mappingResult.array]; 
      } else { 
       // same - log the error if there is one 
       NSLog(@"No?: %@", error); 
      } 
      // same - log the error and keep going 
      [self betterLoadOneWithCompletion:^(RKObjectRequestOperation *op, RKMappingResult *mappingResult, NSError *error) { 
       // same... 
       if (!error) { 
        [self.combinedModel addObjectsFromArray:mappingResult.array]; 
       } else { 
        NSLog(@"No?: %@", error); 
       } 
       [self sortCombinedModel]; 
       [self.tableView reloadData]; 
      }]; 
     }]; 
    }]; 
} 
+0

我明白你的意思,謝謝你畫出一幅清晰的圖畫。我不確定在新的'loadOneWithCompletion:'我在哪裏放置'[self.eObjectManager getObjectsAtPath:路徑參數:無成功:失敗:失敗];''我以前有過的部分,或者我該怎麼做把它變成你有'callSomeExternalLibrary'的部分? – Realinstomp 2014-11-21 00:20:32

+0

@Realinstomp - 我一定錯過了你發佈的如何調用外部API。請參閱建議具體更改的編輯。 – danh 2014-11-21 01:18:28

+0

這是有道理的,快速的問題,我會把'[self sortCombinedModel];'和'[self.tableView reloadData];'?我無法弄清楚它的最佳工作點。以前它在'loadMedia'內部的'loadThreeWithSuccess'中。 – Realinstomp 2014-11-21 01:59:30

2

也許你可以嘗試這樣的事情,首先要定義樹布爾變量:finish1,finish2和finish3

- (void)loadMedia { 

    self.combinedModel = [NSMutableArray array]; 

    [self loadOneWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

     [self.combinedModel addObjectsFromArray:mappingResult.array]; 
     finish1 = true; 
     [self reloadTableData] 

    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"No?: %@", error); 
     finish1 = true; 
     [self reloadTableData] 
    }]; 


    [self loadTwoWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

     [self.combinedModel addObjectsFromArray:mappingResult.array]; 
     finish2 = true; 
     [self reloadTableData] 

    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"No?: %@", error); 
     finish2 = true; 
     [self reloadTableData] 
    }]; 


    [self loadThreeWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

     [self.combinedModel addObjectsFromArray:mappingResult.array]; 
     finish2 = true; 
     [self reloadTableData] 

    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"No?: %@", error); 
     finish3 = true; 
     [self reloadTableData] 
    }]; 

} 

- (void) reloadTableData { 

    if (finish1 && finish2 && finish3) { 
     [self sortCombinedModel]; 
     [self.tableView reloadData]; 
    } 
} 
+0

感謝您的迴應!這看起來不錯,謝謝你的答案! – Realinstomp 2014-11-21 13:16:46

相關問題