0我正在尝试在我的应用首次运行时使用数组预加载核心数据。在viewDidLoad中,它检查核心数据是否为空。如果是,它调用一个方法来加载带有数组的核心数据。我的问题是,当我检查核心数据的内容时(使用Core Data Pro Readonly),核心数据的内容中有一个项目:" NULL"。这是我认为将数组加载到核心数据中的关键线,这给我带来了问题:
newArray = [elementsArray objectAtIndex:0];
核心数据实体被称为"元素"并且有一个属性" elementName"。 这是整个方法的内容:
-(void)loadElementsEntity {
NSLog(@"LOADING ELEMENTS INTO CORE DATA");
NSMutableArray *elementsArray = [[NSMutableArray alloc]init];
// LOAD THE DATA INTO THE ARRAY
elementsArray = [[NSMutableArray alloc] initWithObjects:
@"Wall",
@"Floor",
@"Ceiling",
@"Door",
@"Window",
@"Roof",
@"Electrical",
@"",
@"* New Element *", nil];
// LOAD THE ARRAY INTO CORE DATA: ELEMENTS ENTITY
AppDelegate *aDel = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *moc = [aDel managedObjectContext];
NSManagedObject *newArray;
newArray = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:moc];
newArray = [elementsArray objectAtIndex:0];
NSError *error = nil;
if (![moc save:&error]) { //SAVE FAILED
NSLog(@"YOUR SAVE FAILED! %@", [error localizedDescription]);
} else {
NSLog(@"YOUR SAVE SUCCEEDED.");
}
// CHECK TO SEE IF CORE DATA WAS LOADED
NSLog(@"LISTING THE ARRAY");
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:[NSEntityDescription entityForName:@"Elements" inManagedObjectContext:moc]];
NSArray *testArray = [self.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"ELEMENTSARRAY COUNT: %d", [elementsArray count]);
NSLog(@"LOADED TESTARRAY: count = %d", [testArray count]);
}
有谁可以指出我在哪里错了?
我使用for语句更新了方法的保存部分 但它仍然在核心数据中产生一个NULL条目!
请帮忙!
// LOAD THE ARRAY INTO CORE DATA ELEMENTS
AppDelegate *aDel = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *moc = [aDel managedObjectContext];
NSManagedObject *newString = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:moc];
NSError *error = nil;
for (NSString *elementname in elementsArray) {
NSLog(@"NAME= %@", elementname);
[newString setValue:elementname forKey:@"elementName"];
if (![moc save:&error]) { //SAVE FAILED
NSLog(@"YOUR SAVE FAILED! %@", [error localizedDescription]);
} else {
NSLog(@"SAVE SUCCEEDED.");
}
}
答案 0 :(得分:0)
这一行:
newArray = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:moc];
正在创建一个新的空实体实例。然后,你马上做:
newArray = [elementsArray objectAtIndex:0];
用一个字符串替换对新实体实例的引用。因此,当保存上下文时,您将有一个空对象。
您应该做的是迭代elementsArray
的内容并在每次迭代时创建一个新的实体实例,然后使用该索引处的数组内容来配置新的实体实例。
for (NSString *elementName in elementsArray) {
// create a new entity instance
NSManagedObject *newString = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:moc];
// configure using 'name'
[newString setValue:elementName forKey:@"elementName"];
}
NSError *error = nil;
NSLog(@"LOADING: %@", newString);
if (![moc save:&error]) { //SAVE FAILED
NSLog(@"YOUR SAVE FAILED! %@", [error localizedDescription]);
} else {
NSLog(@"SAVE SUCCEEDED.");
}
答案 1 :(得分:0)
评论有点长,所以这里是答案。
//将阵列加载到核心数据元素
AppDelegate *aDel = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *moc = [aDel managedObjectContext];
NSError *error = nil;
for (NSString *elementname in elementsArray) {
NSManagedObject *element = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:moc];
element.title = elementname; //your attribute name instead title and it must be string
}
if (![moc save:&error]) { //SAVE FAILED
NSLog(@"YOUR SAVE FAILED! %@", [error localizedDescription]);
} else {
NSLog(@"SAVE SUCCEEDED.");
}