如何使用现有NSManaged对象模型添加来自不同位置的数据

时间:2016-07-07 10:54:39

标签: ios objective-c core-data nsmanagedobject

以下是我向CoreData显示数据的方式,这是我通过字典JSON * dict中的NSDictionary从服务器获取的数据。

  NSArray *results = [context executeFetchRequest:request error:&error1];
                    NSLog(@"results:%@",results);

                    NSManagedObject *updateDevice=[results lastObject];

                    if([results count] > 0){
                        //if(1){
                        NSLog(@"updateeeee");
                        //continue;
         [updateDevice setValue:[NSString stringWithFormat:@"%@",[dict objectForKey:@"clip_image_path"]] forKey:@"clip_image_path"];
         [updateDevice setValue:[dict objectForKey:@"clip_name"] forKey:@"clip_name"];
         [updateDevice setValue:[dict objectForKey:@"page_categorisation"] forKey:@"page_categorisation"];
         [updateDevice setValue:[dict objectForKey:@"modifieddate"] forKey:@"modifieddate"];
         [updateDevice setValue:[dict objectForKey:@"id"] forKey:@"id"];
         [updateDevice setValue:[commonvideo stringByAppendingString:[NSString stringWithFormat:@"%@",[dict objectForKey:@"path"] ]]forKey:@"clip_path"];
         [updateDevice setValue:[dict objectForKey:@"description"] forKey:@"descriptions"];
         [updateDevice setValue:[dict objectForKey:@"created_by"] forKey:@"created_by"];
         [updateDevice setValue:[dict objectForKey:@"state"]  forKey:@"state"];

以下是我将代码添加到不同视图的服务器的代码。在这里,我想要做的是,我想将textFields中的数据存储到实体“ClipTable”中的上述属性,因此我遇到了如何更新现有托管对象的问题。单击提交按钮后,此afnetworking i调用此后,它会通过API将信息存储到服务器。现在我想在剪辑上传后更新CoreData模型。很难修复它,感谢任何帮助。

  -(void)afnetworking{

NSString *user_id=[[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];

NSString *urlString =K_ServerAPI;
NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc] init];
[request2 setURL:[NSURL URLWithString:urlString]];
[request2 setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

[request2 addValue:contentType forHTTPHeaderField:@"Content-Type"];

//Your uploading file.
NSMutableData *bodyData = [NSMutableData data];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  // [bodyData appendData:[[[NSUserDefaults standardUserDefaults] objectForKey:@"userID"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[user_id dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"task\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"webapi.insertClip" dataUsingEncoding:NSUTF8StringEncoding]];

下面是nameCliptxt.text我必须更新上面CoreData中的“clip_name” 通过textField中的信息来模拟我必须为下面的所有事情做的明智

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"clip_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[nameCliptxt.text dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"description\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[detailtxtView.text dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"page_categorisation\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[pageIdArray objectAtIndex:[pageCategoryArray indexOfObject:pagecategoryTxtFild.text]] dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"video_category\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[videoIdarray objectAtIndex:[arrayofVideoCategory indexOfObject:videoTextFiled.text]]  dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Disposition: form-data; name=\"video_tag\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[video_Tag.text dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

NSString *strData = [[NSString alloc]initWithData:bodyData encoding:NSUTF8StringEncoding];
NSLog(@"Request body %@",strData);

NSString *videoUrl=[selectedVideoURL path];
if(videoUrl){
    NSString *videoName = [[videoUrl lastPathComponent]stringByDeletingPathExtension];
    NSData *videoData  = [NSData dataWithContentsOfURL:selectedVideoURL];
    [bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *tmpfileinfo = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video_clip\"; filename=\"%@.mp4\"\r\n",videoName];
    [bodyData appendData:[tmpfileinfo dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyData appendData:[NSData dataWithData:videoData]];

}

0 个答案:

没有答案