我有一种情况,我有一些模板(对象),我想将它们推送到我的后端。现在,当用户按下同步按钮时,会连接到我获取所有模板的后端表单。IPAD 应用程序上的模板与从后端传输的模板进行比较。如果 IPAD 应用程序上的模板具有 ID(0),则有一个帖子到后端。然后后端返回保存的模板和他的 ID(添加到本地存储的模板)。
现在我认为我的问题是我对所有这些到后端的帖子使用相同的连接(templateupdateconnection => bad name...)。实际的问题是,在 connectiondidfinishloading 方法中,我只得到了对发布到后端的最后一个模板的响应。
谁知道我该如何解决这个问题?
提前致谢!!
编码
-(void)syncRegistrations:(NSArray *)arrayOfRegistrations{
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:[NSEntityDescription entityForName:@"Registration" inManagedObjectContext:self.managedObjectContext]];
NSError *error;
NSArray *Data = [self.managedObjectContext executeFetchRequest:request error:&error];
BOOL old=FALSE;
for(int registrationCounter = 0; registrationCounter < arrayOfRegistrations.count; registrationCounter ++){
NSDictionary *dictRegistration = [arrayOfRegistrations objectAtIndex:registrationCounter];
for(Registration *registration in Data){
if([dictRegistration objectForKey:@"id"] == registration.id){
old = TRUE;
}
else if ([registration.id intValue]==0){
NSString *jsonRequest = [NSString stringWithFormat:@"{\"form\":%@}",registration.form.id];
NSLog(@" de jsonrequest: %@",jsonRequest);
NSURL *url = [NSURL URLWithString:@"http://mybackend/registrations"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
self.registrationtoupdate = registration;
self.registrationUpdateConnection = NULL;
self.registrationUpdateConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.registrationUpdateConnection start];
registration.id=[NSNumber numberWithInteger:-1];
[self.managedObjectContext save:&error];
old=TRUE;
}
}
if(old==FALSE){
//nieuwe template toevoegen
Registration *registration = [NSEntityDescription insertNewObjectForEntityForName:@"Registration" inManagedObjectContext:self.managedObjectContext];
registration.id = [dictRegistration objectForKey:@"id"];
registration.form = [self getFormByID:[dictRegistration objectForKey:@"form"]];
[self.managedObjectContext save:&error];
}
old=FALSE;
}
//[self getRC];
[self performSelector:@selector(getRC) withObject:nil afterDelay:3];
}
CONNECTIONDIDFINISH加载方法
else if([connection isEqual:self.registrationUpdateConnection]){
NSDictionary *dictRegistration = [NSJSONSerialization JSONObjectWithData:self.registrationdata options:kNilOptions error:&error];
NSLog(@"de data van de registratie is: %@",dictRegistration);
NSLog(@"de registration to update is: %@",self.registrationtoupdate);
self.registrationtoupdate.id = [dictRegistration objectForKey:@"id"];
[self.managedObjectContext save:&error];
}