NSManagedObject没有进入模型

时间:2011-05-23 17:12:21

标签: objective-c core-data nsmanagedobject

我使用以下代码尝试在模型中上传NSManagedObject(可能是不正确的术语)。

请告诉我这是什么问题。

CDImage *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:[self managedObjectContext]];
    [image setDate:[NSDate date]];
    [image setImage:UIImagePNGRepresentation([_imageView image])];
    [image setLocation:[dict objectForKey:@"location"]];
    [image setUrl:[dict objectForKey:@"url"]];

CDImage.m

@synthesize url, date, image, location;
- (void) dealloc {
    [url release];
    [date release];
    [image release];
    [location release];
    [super dealloc];
}

我的模特:

enter image description here

获取对象:

- (void) refreshArray {
    if ([[self cdArray] isEqual:nil]) {
        [self setCdArray:[[NSMutableArray alloc] init]];
    }
    [cdArray removeAllObjects];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSError *error = nil;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Image" 
                                              inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];    
    for (CDImage *image in fetchedObjects) {
        [cdArray addObject:image];
        [[self tableView] reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[cdArray indexOfObject:image] inSection:0]] withRowAnimation:UITableViewScrollPositionMiddle];
    }
    [[self tableView] reloadData];
    NSLog(@"%d", [cdArray count]);
    NSLog(@"IN REFRESH ARRAY");
    [fetchRequest release];
}

1 个答案:

答案 0 :(得分:3)

您需要在写完

后保存上下文
NSError *saveError;        
if (![context save:&saveError]) {
    NSLog(@"Error saving entity: %@", saveError);
}