gpt4 book ai didi

ios - 核心数据保存不持久

转载 作者:行者123 更新时间:2023-11-29 02:12:46 25 4
gpt4 key购买 nike

我正在尝试从我的 UICollectionView 中删除一个项目。该项目也使用 CoreData 保存。在我重新加载 View 或重新启动应用程序之前,删除该项目似乎有效。两者都在下面调用 getCards。当发生这种情况时,我的项目又回来了,甚至在 Core Data 数据库中,该对象似乎还没有被删除。

代码:

    -(void)removeCard:(int)position{
UserModel *selectedUser = [self getSelectedUserFromDB];

CardModel *cardToRemove;
for(CardModel *cardmodel in selectedUser.cards){
if(cardmodel.position.intValue == position){
cardToRemove = cardmodel;
break;
}
}
int positionOldCard = cardToRemove.position.intValue;
[selectedUser removeCardsObject:cardToRemove];
NSMutableArray *cards = [selectedUser.cards.array mutableCopy];
NSLog(@"CARDSCOUNT: %d", cards.count);
//This changes the position of the cards to accomodate the removing of cards above
for(CardModel *cardmodel in cards){
if(cardmodel.position.intValue > positionOldCard){
[cardmodel setPosition:[NSNumber numberWithInt:(cardmodel.position.intValue - 1)]];
[selectedUser replaceObjectInCardsAtIndex:cardmodel.position.intValue withObject:cardmodel];
}
}

NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Removing the card failed %@", error);
}

NSLog(@"Saved cards: %d", [self getCards].count);
}
-(NSMutableArray *)getCards{
UserModel *selectedUser = [self getSelectedUserFromDB];
NSMutableArray *cards = [[NSMutableArray alloc] init];
for(CardModel *cardModel in selectedUser.cards){
[cards addObject:[self modelToCard:cardModel]];
}
NSLog(@"Loaded cards: %d", cards.count);
return cards;
}

输出:

2015-03-17 11:13:56.259 BeNext[3593:349310] Loaded cards: 4
2015-03-17 11:13:56.260 BeNext[3593:349310] Saved cards: 4
2015-03-17 11:13:56.271 BeNext[3593:349310] Loaded cards: 4
//RELOADING VIEW
2015-03-17 11:14:02.351 BeNext[3593:349310] Loaded cards: 5

最佳答案

如果您想删除卡片,您需要在调用 save 之前调用 [context deleteObject:card model] - 您正在操纵关系,但这不是与实际删除卡相同。

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

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