0

我有一种情况,我有一些模板(对象),我想将它们推送到我的后端。现在,当用户按下同步按钮时,会连接到我获取所有模板的后端表单。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];


}
4

2 回答 2

1

您应该将所有相关的状态数据封装NSURLConnection到一个类中。让那个类有一个start异步启动连接的方法和一个完成块,它有一个参数result代表请求的最终结果。这样,您就不会将响应数据与其他连接不匹配。

然后,在完成块中,引用相应的上下文(要更新的注册),获取 JSON 表示并为给定的托管对象上下文适当地更新正确线程或队列上的上下文(参见下面的“线程限制”):

所以,基本上:

        else if ([registration.id intValue]==0) {
            // setup the request
            NSMutableURLRequest *request =  ...
            MyHTTPRequestOperation* op = 
                [[MyHTTPRequestOperation alloc] initWithRequest:request
                                                     completion:^(void)(id result)              
                {
                    if (![result isKindOfClass:[NSError class]]) {
                        assert(result != nil && [result isKindOfClass:[NSData class]]);
                        NSDictionary* dictRegistration = 
                           [NSJSONSerializationJSONObjectWithData:result 
                                                          options:kNilOptions 
                                                            error:&error];
                        // Todo: check if dictRegistration has the required type 

                        id newRegistrationID = [dictRegistration objectForKey:@"id"];

                        // Update the model on the thread/queue you defined for the 
                        // NSManagedObjectContext:
                        [self updateRegistration:registration withID: newRegistrationID];
                    }
                    else {
                        // an error occurred
                    }                        
                }];
            [op start];

            registration.id=[NSNumber numberWithInteger:-1];
        }

你应该熟悉 NSModelObjectContext 和“线程限制”。

也可以看看: -(id)initWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct

OS X v10.7 和 iOS 5.0 的核心数据发行说明

WWDC/2012上的核心数据最佳实践 (需要开发帐户)

于 2013-06-12T15:36:40.840 回答
0

@CouchDeveloper,感谢您让我走上正轨!!

最终的解决方案不是创建 NSURLConnection 的子类,而是将完成块与简单的 NSURLConnection 结合使用。检查以下代码:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request  queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     if ([data length] > 0 && error == nil){

         NSDictionary *dictrc = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
         NSLog(@"de data van de rc is: %@",dictrc);
         rc.id = [dictrc objectForKey:@"id"];

         [self.managedObjectContext save:&error];
     }


 }];
于 2013-06-15T10:31:21.897 回答