我在这里的第一篇文章,直截了当 .
我遇到了一个问题,我不确定是保存数据还是获取数据 . 这是我的代码,我试图在我的实体中保存数据,“序列” . 该实体由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);
}
当我重新启动应用程序时出现问题 . 数组要么显示(null)两个数组,要么显示两个数组的内容 . 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”类型的属性中 .
1 回答
经过研究,我设法解决了这个问题 . 我不确定它是如何工作的但我通过在我尝试获取数据时重新排列代码来解决它 .
我也尝试了两种保存数据的方法 . 在我试图保存数据的_2664250中,我将
NSMutableArray
转换为NSArray
.我试过的第二种方法是使用Core Data not saving changes to Transformable property的回答
显然它以某种方式起作用 .