【发布时间】:2017-05-31 08:10:17
【问题描述】:
我在这里的第一篇文章,直奔主题。
我遇到了一个问题,我不确定是保存数据还是获取数据。 这是我尝试将数据保存在我的实体“序列”中的代码。该实体由 2 个属性“seqForWk1CD”和“seqForWk2CD”组成。
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
SequenceMO *seqEntity = [NSEntityDescription
insertNewObjectForEntityForName:@"Sequence"
inManagedObjectContext:context];
// some other code
seqEntity.seqForWk1CD = arrForWk1;
seqEntity.seqForWk2CD = arrForWk2;
NSLog(@"%@", arrForWk1);
NSLog(@"%@", arrForWk2);
NSError *error = nil;
if (context != nil) {
if ([context hasChanges] && ![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
打印时,数组将始终显示数组的内容。
这是我尝试获取数据的地方。
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sequence" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error;
if(![context save:&error]){
NSLog(@"Error fetching. %@ %@", error, [error localizedDescription]);
}
NSUInteger count = [context countForFetchRequest:request error:&error];
if(count != 0){
NSArray *fetchObj = [context executeFetchRequest:request error:&error];
NSManagedObject *sequence = (NSManagedObject *)[fetchObj objectAtIndex:0];
NSLog(@"1 - %@", sequence);
arrForWk1 = [sequence valueForKey:@"seqForWk1CD"];
NSLog(@"%@", arrForWk1);
arrForWk2 = [sequence valueForKey:@"seqForWk2CD"];
NSLog(@"%@", arrForWk2);
}
当我重新启动应用程序时出现问题。数组要么显示(空)两个数组,要么显示两个数组的内容。 if(![context save:&error]) 的 if 语句永远不会被触发。已经为实体添加了 NSManagedObject 的子类。
我也尝试在@interface 中声明 AppDelegate 并通过 [delegate saveContext]; 强制立即保存上下文。
这是进行保存的方法。 "check" 在 viewDidLoad 方法中被初始化为 0。 “arrForWk1”和“arrForWk2”都在@interface 声明。
- (IBAction)randomizeSequence:(UIButton *)sender {
NSMutableArray *storeArray = [[NSMutableArray alloc] init];
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
SequenceMO *seqEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Sequence" inManagedObjectContext:context];
BOOL record = NO;
int x;
for (int i=0; [storeArray count] < 9; i++) //Loop for generate different random values
{
x = 1 + arc4random() % 9;//generating random number
if(i==0)//for first time
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
else
{
for (int j=0; j<= [storeArray count]-1; j++)
{
if (x ==[[storeArray objectAtIndex:j] intValue])
record = YES;
}
if (record == YES)
{
record = NO;
}
else
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
}
}
check++;
if(check == 1 ) {
arrForWk1 = storeArray;
[self.wk1Seq reloadData];
}
else if(check == 2) {
arrForWk2 = storeArray;
seqEntity.seqForWk1CD = arrForWk1;
seqEntity.seqForWk2CD = arrForWk2;
NSError *error = nil;
if (context != nil) {
if ([context hasChanges] && ![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
[self.wk2Seq reloadData];
}
补充一点,数组的数据类型是 NSMutableArray,我正在尝试将它们存储到“Transformable”类型的属性中。
【问题讨论】:
-
插入对象后是否保存了上下文?
-
什么时候调用代码的第一个sn-p?
-
您的 storeArray 不包含任何内容。每次单击按钮时都会初始化 storeArray,并尝试将该数组保存在不包含任何对象的 coreData 中。
-
请仅在需要时初始化上下文,即检查值为2时
标签: ios objective-c core-data