我在我的应用程序中使用MagicalRecord和CoreData Wrapper实现了最喜欢的场景。我正在搜索价值,如果在我的实体中找到该属性,它会将其删除为fav,否则它会将其添加到fav。在这里DX *foundDX = [DX MR_findFirstByAttribute:@"code" withValue:cell.DXCodeName.text];
foundDX是我正在检查的值。
我已经在我的tableView中添加了一个UISearchBar。因此,当加载视图时,它会显示已使用可能不喜欢的fav项目。如果用户搜索并选择一个项目,它会成功将其添加到fav并成功将其从数据模型中删除。但如果我再次点击“收藏”按钮,它就会崩溃。
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按钮,foundDX仍然是E45
,因此它会崩溃。但是如果我关闭应用并再次运行它E45
就不再存在了。
的更新
我已经更新了上面的代码。我在fetchFavDX
中使用viewDidLoad
,因此我尝试在上面的代码中更新的方法中调用它。而在fetchFavDX
我这样做:
-(void)fetchFavDX {
self.favDXArray = [NSMutableArray arrayWithArray:[DX MR_findAllSortedBy:@"code" ascending:YES]];
}
现在它在前两次工作正常,这意味着添加/删除/添加/删除。之后它只运行块if (!foundDX.code)
,这意味着它不会进入else部分。
答案 0 :(得分:1)
你应该检查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的方法正在返回您期望的内容。同时确保添加/删除使您处于预期的状态,即您拥有已添加或删除的内容