UNIQUE CONSTRAINT FAILED:ZCLIPTABLE.Z_PK

时间:2016-05-16 13:01:19

标签: ios multithreading

当我尝试从菜单屏幕上的核心数据中获取剪辑时,我收到此错误。在到达菜单屏幕时它崩溃

  - (void)viewDidLoad {
         [super viewDidLoad];
        NSString *post = [NSString stringWithFormat:@"task=webapi.getAllClips"];
        [protocolTest  syncAllClips:post andEntityName:@"ClipTable"];
        }

这里当我从下面的其他类调用syncAllClips方法时崩溃是方法代码 -

-(void)syncAllClips:(NSString*)postString andEntityName:(NSString*)entityName{

 NSLog(@"loop1");
 incline=0;
 [delegateInstance deleteCoreData:entityName];

NSManagedObjectContext *context = [delegateInstance managedObjectContext];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
configuration.timeoutIntervalForRequest = 3 * 60.0;
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:[self postRequest:postString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    }
          else
        {
        if (data){

            NSString *_str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSString *_escapedString = [_str stringByReplacingOccurrencesOfString:@"\\'" withString:@"'"];
            //_escapedString = [_escapedString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"@@"];
            NSData *_escData = [_escapedString dataUsingEncoding:NSUTF8StringEncoding];

            NSDictionary *serverdata = [NSJSONSerialization JSONObjectWithData:_escData options:NSJSONReadingMutableContainers error:NULL];

           // NSArray *jsonDataArray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:_escData options:NSJSONReadingMutableContainers error:NULL]];
           // NSMutableArray *jsonDataArray = [NSMutableArray new];
            NSMutableArray * jsonDataArray = (NSMutableArray *)[serverdata objectForKey:@"task"];

            //jsonDataArray =[serverdata objectForKey:@"task"];
        dataArray = [[NSMutableArray alloc] init];
    NSLog(@"serverdata is%@",serverdata);
             NSLog(@"jsonDataArray is%@",jsonDataArray);
           NSLog(@"datetime_value: %@", [serverdata objectForKey:@"datetime_value"]);
            NSLog(@"datetime_value: %@", [serverdata objectForKey:@"total_clips_count"]);
            //NSLog(@"serverdata :%@",serverdata);
            NSString *commonvideo=[NSString stringWithFormat:@"%@",[serverdata objectForKey:@"video_clip_path"]];
            NSLog(@"commonvideo is%@",commonvideo);

            for(NSDictionary *dict in jsonDataArray)
            {
            incline++;


                // NSLog(@"serverdata :%@",[dict objectForKey:@"clip_image_path"]);
                // [datarara addObject:[dict objectForKey:@"clip_image_path"]];
                // Create a new device
                NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
                [newDevice setValue:[NSString stringWithFormat:@"%@",[dict objectForKey:@"clip_image_path"]] forKey:@"clip_image_path"];
                [newDevice setValue:[dict objectForKey:@"clip_name"] forKey:@"clip_name"];
                [newDevice setValue:[dict objectForKey:@"page_categorisation"] forKey:@"page_categorisation"];
                [newDevice setValue:[dict objectForKey:@"modifieddate"] forKey:@"modifieddate"];
                [newDevice setValue:[dict objectForKey:@"id"] forKey:@"id"];
                [newDevice setValue:[commonvideo stringByAppendingString:[NSString stringWithFormat:@"%@",[dict objectForKey:@"path"] ]]forKey:@"clip_path"];
                [newDevice setValue:[dict objectForKey:@"description"] forKey:@"descriptions"];
                 //   [newDevice setValue:[NSString stringWithFormat:@"%d",incline] forKey:@"tabid"];
                [dataArray addObject:[dict objectForKey:@"clip_name"]];

                //NSLog(@"name is%@",[dict objectForKey:@"clip_name"]);

                NSError *error = nil;

                // Save the object to persistent store
                    if (![context save:&error]) {
                        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
                    }
                }
            NSLog(@"dataarray is%@",dataArray);
            NSLog(@"dataarray count is%lu",(unsigned long)dataArray.count);
            [[self delegate]getsyncdata:YES andDate:[serverdata objectForKey:@"datetime_value"] andtotalVideoCount:[serverdata objectForKey:@"total_clips_count"] andgetVideoCount:[NSString stringWithFormat:@"%lu",(unsigned long)dataArray.count]];
        }
    }
  }];
    [postDataTask resume];
}

并且在此之下有相同的方法syncAllClips1与相同的代码试图获取其他50个视频。以这种方式共有150个剪辑,这些拖曳方法背靠背调用50,50个剪辑

0 个答案:

没有答案