以下是实体Player
头文件。
@interface Player : NSManagedObject
@property (nonatomic, retain) NSNumber * experience;
@property (nonatomic, retain) id items;
@property (nonatomic, retain) NSNumber * level;
@end
@interface items : NSValueTransformer
@end
items
本质上是一个NSMutableArray
withNSNumber
元素。在一个函数中,我正在更新这个数组:
- (void)itemWasDropped:(ItemIndex)item
{ // _player has been correctly retrieved from the database
// the current number of this item
int nNum = [[_player.items objectAtIndex:item] intValue];
// to increment the number
[_player.items replaceObjectAtIndex:item withObject:[NSNumber numberWithInt:nNum + 1]];
}
这个函数被完美地调用。我将上下文保存在viewWillDisappear
函数中。
- (void)viewWillDisappear:(BOOL)animated
{ // _moc is an NSManagedObjectContext instance that has been correctly initialised.
// to update the database
NSError* err = nil;
BOOL bSucc = [_moc save:&err];
if (err || !bSucc)
{
...
}
}
问题是只要我不关闭任务栏中的应用程序,更新在其他视图中是可见的。有什么问题?任何人都可以帮忙吗?