问题描述
我有三个的我拉的数据,并放入
C $ C> ViewController.m 。的UITableView
我的<$内部API
有没有办法仍然让的UITableView
负荷,如果其中一个网站没有加载?
Is there a way to still let the UITableView
load if one of the websites isn't loading?
现在, ViewController.m
只是如果所有3个来源是不是按我的方法加载不加载。
Right now, the ViewController.m
just doesn't load if all 3 sources aren't loading per my method.
下面是我使用的方法:
- (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
不加载, API2
和 API3
仍将加载和显示在的UITableView
ViewController.m
。
So if API1
doesn't load, API2
and API3
will still load and show in the UITableView
in ViewController.m
.
推荐答案
该loadOne,loadTwo ...功能有一个缺点是,他们需要两个块参数,一个是成功的,一个为断电。如果你改变那些需要处理成功或失败的一个块,这将是更容易出现错误后进行。
The loadOne, loadTwo ... functions have a disadvantage which is that they take two block parameters, one for success and one for fail. If you change those to take a single block that handles success or failure, it will be much easier to carry on after errors occur.
修改更改您如何通过不直接传递的完成和失败块打电话给你eObjectManager。相反,实施这些块和重新排列PARAMS相匹配的单块接口...
EDIT Change how you call your eObjectManager by not directly passing on the completion and failure blocks. Instead, implement those blocks and rearrange the params to match the single block interface...
- (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和一个零NSError,或为结果参数零和一个错误的一个实例。有了这个API,我们可以很容易地解决你的方法,因为它们发生,只是记录错误,并坚持下去,错误或不...
It can still call your old function or some external library with two blocks, but it combines the result into a single block. The caller of this expects that they will either get a good RKMappingResult and a nil NSError, or a nil for the result parameter and an instance of an error. With this api, we can easily fix your method to just log errors as they occur and carry on, error or not...
- (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];
}];
}];
}];
}
这篇关于加载视图控制器,即使一个API失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!