gpt4 book ai didi

ios - 在核心数据中保存/获取数据不一致

转载 作者:行者123 更新时间:2023-12-01 16:14:48 25 4
gpt4 key购买 nike

我在这里的第一篇文章,直达重点。

我遇到了问题,不确定是否保存数据或获取数据。
这是我尝试在实体“序列”中保存数据的代码。该实体由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];来立即保存上下文,这是保存的方法。在viewDidLoad方法中,“check”被初始化为0。 @ar中声明了“arrForWk1”和“arrForWk2”。
- (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”类型的属性中。

最佳答案

经过研究,我设法解决了这个问题。我不确定它的实际工作方式,但是我在尝试获取数据时通过重新排列代码来解决了它。

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSEntityDescription *descriptor = [NSEntityDescription entityForName:@"Sequence" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
request.entity = descriptor;
NSError *error;
NSArray *fetchObj = [context executeFetchRequest:request error:&error];
if(fetchObj == nil) {
NSLog(@"Error occured when trying to fetch.");

}
else {
if(fetchObj.count == 0) {
NSLog(@"No objects saved");
else {
NSManagedObject *sequence = (NSManagedObject *)[fetchObj objectAtIndex:0];
NSLog(@"1 - %@", sequence);
arrForWk1 = [sequence valueForKey:@"seqForWk1CD"];
NSLog(@"%@", arrForWk1);
arrForWk2 = [sequence valueForKey:@"seqForWk2CD"];
NSLog(@"%@", arrForWk2);
NSLog(@"2 - %@", sequence);
}

我试过也尝试了两种保存数据的方法。在尝试保存数据的 if statement中,我将 NSMutableArray转换为 NSArray
else if(check == 2) {
test2 = storeArray;
NSManagedObjectContext *context = [delegate managedObjectContext];
SequenceMO *seqEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Sequence" inManagedObjectContext:context];
NSArray *tArr = [arrForWk1 copy];
NSArray *tArr2 = [arrForWk2 copy];
seqEntity.seqForWk1CD = tArr;
seqEntity.seqForWk2CD = tArr2;
NSError *error = nil;
if (context != nil) {
if ([context hasChanges] && ![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}

我尝试的第二种方法是使用 Core Data not saving changes to Transformable property的答案
id temp = [seqEntity seqForWk1CD];
id temp2 = [seqEntity seqForWk2CD];
temp = arrForWk1;
temp2 = arrForWk2;
[seqEntity setSeqForWk1CD:temp];
[seqEntity setSeqForWk2CD:temp2];

显然,它以某种方式起作用。

关于ios - 在核心数据中保存/获取数据不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44279148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com