gpt4 book ai didi

ios - Removed Entity 再次被 MagicalRecord 中的 findFirstByAttribute 找到

转载 作者:行者123 更新时间:2023-11-29 00:39:38 28 4
gpt4 key购买 nike

我正在使用 MagicalRecord 和 CoreData Wrapper 在我的应用程序中使用 Favorite 场景。我正在搜索的值是,如果在我的实体中找到该属性,它会将其作为收藏夹删除,否则会将其添加到收藏夹。在此处执行操作到我的 TableView 。因此,当加载 View 时,它会显示已经收藏的项目,这些项目可以取消收藏。如果用户搜索并选择一个项目,它会成功地将其添加到收藏夹并成功地将其从数据模型中删除。但是,如果我再次点击收藏按钮,它就会崩溃。

   if (!foundDX.code) {
//If we are always getting into here that means that either foundDX is nil
//or the code is nil so you need to verify the logic around that.
if (indexPath != nil)
{
if (!_AddEditDX) {
self.AddEditDX = [DX MR_createEntity];
}
[self.AddEditDX setCode:cell.DXCodeName.text];
[self.AddEditDX setDescriptions:cell.DXDescText.text];
[self.AddEditDX setIsFav:[NSNumber numberWithInt:1]];

[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
[self.favDXArray addObject:self.AddEditDX];
[self fetchFavDX];

[cell.DXFavButton setImage:[UIImage imageNamed:@"star_filled"] forState:UIControlStateNormal];
[KSToastView ks_showToast:@"Added to Favourite" duration:1.0f];

}
} else if(self.favDXArray.count > indexPath.row) {
//You can get rid of the if(foundDX.code) because this is the else block to not having it so we must have it.
//Only thing to worry about is the array size
NSLog(@"count is: %ld and row is: %ld",_favDXArray.count, (long)indexPath.row);
DX *tempDX = [self.favDXArray objectAtIndex:indexPath.row];
[tempDX MR_deleteEntity];

[cell.DXFavButton setImage:[UIImage imageNamed:@"star_blank"] forState:UIControlStateNormal];
[self.favDXArray removeObjectAtIndex:indexPath.row];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
[KSToastView ks_showToast:@"Removed from Favourite" duration:1.0f];
}

通过应用一些断点我发现:
假设代码是E45并且foundDX没有第一次找到它并添加到数据中模型。然后再次按下 fav 按钮将其删除,实际上它确实将其删除。但是,如果我再次点击 fav 按钮,foundDX 仍然是 E45,因此它会崩溃。但是如果我关闭应用程序并再次运行它,E45 将不再存在。

更新
我已经更新了上面的代码。我在 viewDidLoad 中使用了 fetchFavDX,所以我尝试在上面代码中更新的方法中调用它。而在 fetchFavDX 中,我这样做:

-(void)fetchFavDX {
self.favDXArray = [NSMutableArray arrayWithArray:[DX MR_findAllSortedBy:@"code" ascending:YES]];
}

现在它在前两次工作正常,这意味着添加/删除/添加/删除。之后,它只运行 if (!foundDX.code) block ,这意味着它不会进入 else 部分。

最佳答案

您应该检查 self.favDXArray 计数是否比 indexpath.row 长,以便实际删除/检索某些内容。它会是这样的:

if(self.favDXArray.count > indexPath.row){
DX *tempDX = [self.favDXArray objectAtIndex:indexPath.row];
[tempDX MR_deleteEntity];
[cell.DXFavButton setImage:[UIImage imageNamed:@"star_blank"] forState:UIControlStateNormal];
[self.favDXArray removeObjectAtIndex:indexPath.row];
[KSToastView ks_showToast:@"Removed from Favourite" duration:1.0f];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

更新

if (!foundDX.code) {
//If we are always getting into here that means that either foundDX is nil
//or the code is nil so you need to verify the logic around that.
if (indexPath != nil)
{
self.AddEditDX = [DX MR_createEntity];
[self.AddEditDX setCode:cell.DXCodeName.text];
[self.AddEditDX setDescriptions:cell.DXDescText.text];
[self.AddEditDX setIsFav:[NSNumber numberWithInt:1]];

[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
[self.favDXArray addObject:self.AddEditDX];
[self fetchFavDX];

[cell.DXFavButton setImage:[UIImage imageNamed:@"star_filled"] forState:UIControlStateNormal];
[KSToastView ks_showToast:@"Added to Favourite" duration:1.0f];

}
} else if(self.favDXArray.count > indexPath.row) {
//You can get rid of the if(foundDX.code) because this is the else block to not having it so we must have it.
//Only thing to worry about is the array size
NSLog(@"count is: %ld and row is: %ld",_favDXArray.count, (long)indexPath.row);
DX *tempDX = [self.favDXArray objectAtIndex:indexPath.row];
[tempDX MR_deleteEntity];

[cell.DXFavButton setImage:[UIImage imageNamed:@"star_blank"] forState:UIControlStateNormal];
[self.favDXArray removeObjectAtIndex:indexPath.row];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
[KSToastView ks_showToast:@"Removed from Favourite" duration:1.0f];
}

确保获取 foundDX 的方法返回您所期望的。还要确保添加/删除让您处于您期望的状态,即您拥有已添加或删除的内容

关于ios - Removed Entity 再次被 MagicalRecord 中的 findFirstByAttribute 找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39831947/

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