我正在尝试保存三个不同的对象,然后用它们中的三个创建一个联合表。我正在使用AFNetworking通过rest api创建它们。我的问题是,因为所有的对象都在数组中,我想在索引1,2,3处创建三个对象的联合表...为了做到这一点,我已经习惯了下面的代码。问题是我需要团队保存,运动保存,然后保存期间,然后创建联合表,然后转到下一个索引路径。目前,它有时会在保存到联合表之前移动到下一个索引路径,因此我得到了错误的值。所以,我正在寻找一种更好的方法来做到这一点,因为当前的方式非常糟糕。
以下是我目前使用的代码:
-(void)saveSportTeam {
for (int i=0; i<self.sportsArray.count; i++) {
id sportObject = self.sportsArray[i];
id teamObject = self.teamsArray[i];
id genderObject = self.genderArray[i];
self.teamString = nil;
self.sportString = nil;
self.genderString = nil;
//
//Team
HttpClientSubclass *httpClient = [HttpClientSubclass sharedClient];
NSDictionary *teamParams = [NSDictionary dictionaryWithObjectsAndKeys:
teamObject, @"team[name]",
nil];
NSMutableURLRequest *teamRequest = [httpClient requestWithMethod:@"POST"
path:@"api/team"
parameters:teamParams];
AFJSONRequestOperation *teamOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:teamRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.teamString = [[NSString alloc] init];
self.teamString = [JSON valueForKeyPath:@"id"];
NSLog(@"Here is the team id: %@", self.sportString);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failure Because %@",[error userInfo]);
NSDictionary *options = @{
//kCRToastNotificationTypeKey : @(CRToastTypeNavigationBar),
kCRToastNotificationPresentationTypeKey : @(CRToastPresentationTypeCover),
kCRToastTextKey : @"We are having problems, please try again later",
kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
kCRToastBackgroundColorKey : [UIColor colorWithRed:(255/255.5) green:(59/255.0) blue:(48/255.0) alpha:1.0],//[UIColor redColor], [UIColor colorWithRed:(255/255.5) green:(59/255.0) blue:(48/255.0) alpha:1.0],
kCRToastAnimationInTypeKey : @(CRToastAnimationTypeLinear),
kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeLinear),
kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionTop)
};
[CRToastManager showNotificationWithOptions:options
completionBlock:^{
NSLog(@"Completed");
}];
}];
[teamOperation start];
//
//Sport
NSDictionary *sportParams = [NSDictionary dictionaryWithObjectsAndKeys:
sportObject, @"sport[name]",
nil];
NSMutableURLRequest *sportRequest = [httpClient requestWithMethod:@"POST"
path:@"api/sport"
parameters:sportParams];
AFJSONRequestOperation *sportOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:sportRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.sportString = [[NSString alloc] init];
self.sportString = [JSON valueForKeyPath:@"id"];
NSLog(@"Here is the sport id: %@", self.sportString);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failure Because %@",[error userInfo]);
NSDictionary *options = @{
//kCRToastNotificationTypeKey : @(CRToastTypeNavigationBar),
kCRToastNotificationPresentationTypeKey : @(CRToastPresentationTypeCover),
kCRToastTextKey : @"We are having problems, please try again later",
kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
kCRToastBackgroundColorKey : [UIColor colorWithRed:(255/255.5) green:(59/255.0) blue:(48/255.0) alpha:1.0],//[UIColor redColor], [UIColor colorWithRed:(255/255.5) green:(59/255.0) blue:(48/255.0) alpha:1.0],
kCRToastAnimationInTypeKey : @(CRToastAnimationTypeLinear),
kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeLinear),
kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionTop)
};
[CRToastManager showNotificationWithOptions:options
completionBlock:^{
NSLog(@"Completed");
}];
}];
[sportOperation start];
//
//Gender
NSDictionary *genderParams = [NSDictionary dictionaryWithObjectsAndKeys:
genderObject, @"gender[name]",
nil];
NSMutableURLRequest *genderRequest = [httpClient requestWithMethod:@"POST"
path:@"api/gender"
parameters:genderParams];
AFJSONRequestOperation *genderOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:genderRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.genderString = [[NSString alloc] init];
self.genderString = [JSON valueForKeyPath:@"id"];
NSLog(@"Here is the sport id: %@", self.genderString);
[self createSportTeam];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failure Because %@",[error userInfo]);
NSDictionary *options = @{
//kCRToastNotificationTypeKey : @(CRToastTypeNavigationBar),
kCRToastNotificationPresentationTypeKey : @(CRToastPresentationTypeCover),
kCRToastTextKey : @"We are having problems, please try again later",
kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
kCRToastBackgroundColorKey : [UIColor colorWithRed:(255/255.5) green:(59/255.0) blue:(48/255.0) alpha:1.0],//[UIColor redColor], [UIColor colorWithRed:(255/255.5) green:(59/255.0) blue:(48/255.0) alpha:1.0],
kCRToastAnimationInTypeKey : @(CRToastAnimationTypeLinear),
kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeLinear),
kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionTop)
};
[CRToastManager showNotificationWithOptions:options
completionBlock:^{
NSLog(@"Completed");
}];
}];
[genderOperation start];
}
[self saveGrade];
}
答案 0 :(得分:0)
使用块连续做四件事的方法是嵌套,但这会产生一些可怕的来源:
[doThing0:^(id result0) {
[doThing1:^(id result1) {
// etc
没有所有嵌套很难看到你的代码,所以这不好。我更喜欢的是做一个待办事项列表。将要连接的三个对象可以单独保存,因此您可以执行以下操作(伪代码):
- (void)doRequests:(NSArray *)requests whenDone:(void (^)(NSArray *)done {
NSMutableArray *todoList = [requests mutableCopy];
NSMutableArray *results = [NSMutableArray array];
for (NSMutableURLRequest *request in requests) {
[AFJSONRequestOperation JSONRequestOperationWithRequest:sportRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if (JSON) [results addObject:JSON];
[todoList removeObject:request];
if (!todoList.count) {
done(results);
}
}
}
现在,您可以使用发布的代码构建所有三个请求。而不是立即运行它们,将请求放在一个数组中。然后...
[self doRequests:@[teamRequest, genderRequest, whateverRequest] whenDone:^(NSArray *results) {
// if the results array looks good, here you can post to the fourth table.
}];