無論何時插入/編輯記錄,您都可以使用NSOperation在後臺執行插入併發布通知。將偵聽器添加到您要顯示數據的View控制器。
所以無論何時控制器收到通知,它都會調用該方法從數據庫中重新加載數據。
@implementation MyClass
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (id) init
{
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"COREDATA_OBJECT_EDITED" object:nil];
return self;
}
- (void) reloadData:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"COREDATA_OBJECT_EDITED"])
{
//load records from database and reload tableview
}
}
@end
//Method where you are saving data objects in some other class
- (void) saveDataObject
{
//Save Data object, if saved successfully then post notification to listener to reload the data
// All instances of MyClass will be notified
[[NSNotificationCenter defaultCenter] postNotificationName:@"COREDATA_OBJECT_EDITED" object:self];
}
你能告訴我一些這方面的教程。我是這款iPhone開發的新手。用一個例子編輯回答 –
。希望這會幫助你。 –
感謝您節省我的一天 –