我正在尝试从pushIncompleteDataToServer
拨打performSelectorInBackground
。它挂起了应用程序。看起来有些死锁问题。我不知道如何解决,因为它是第一次在ios上使用Threads。它挂在__Semwait_signal
系统文件中。
如何解决这个问题。 的的Class1
MatchDayDataController *sharedDataController = [MatchDayDataController sharedDataController];
[sharedDataController performSelectorInBackground:@selector( pushIncompleteDataToServer) withObject:nil];
MatchDayDataController类
-(void) pushIncompleteDataToServer
{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSError *error;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MatchDayChecklist" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"submited == %@" , @2];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSArray *record = [context executeFetchRequest:fetchRequest error:&error];
if (record.count>0)
{
for (int i =0; i<record.count; i++)
{
NSDictionary *afl = [self getFormatedDictionary:[record objectAtIndex:i]];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:afl options:0 error:&error]; // Use options:NSJSONWritingPrettyPrinted to help with debugging JSON string
if (!jsonData)
{
NSLog(@"Error with JSON data creation: %@", error);
return;
}
// put it the fetched record in matchdaychecklist instacnce so to update submit =2 to 1 when submission is sucessful.
MatchDayChecklist *matchdata = [record objectAtIndex:i];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Created JSON string: %@", jsonString);
NSString* jsonDataLength = [NSString stringWithFormat:@"%d", [jsonString length]];
NSURL* url = [NSURL URLWithString:@"ome urls"];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-type"];
[request setValue:jsonDataLength forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* response, NSData* data, NSError* error)
{
if (error != nil)
{
NSLog(@"Submit failed due to: %@", error.localizedDescription);
matchdata.submited = @1;
NSLog(@"update for : %@" ,matchdata);
if (![context save:&error])
{
NSLog(@"Error saving data.");
}
return ;
}
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//data returned not in the format expected - failure
if (dic == nil) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//failure();
NSLog(@"Authentication service returned unexpected data: %@", str);
return;
}
//check if an error has been returned
NSString *err = [dic objectForKey:@"message"]; //dic[@"error"]
if (err != nil) {
// failure();
NSLog(@"Submit failed due to: %@", dic[@"error"]);
matchdata.submited = @1;
NSLog(@"update for : %@" ,matchdata);
if (![context save:&error])
{
NSLog(@"Error saving data.");
}
return;
}
NSLog(@"Submit Successful");
// Submit is success. so turn submit =2 =>submit =1 and save the context.
// matchdata.submited = @1;
// NSLog(@"update for : %@" ,matchdata);
// if (![context save:&error])
// {
// NSLog(@"Error saving data.");
// }
}];
}
}
}