[英]Removing a binary attribute's data(used in NSImageView) in Core Data entity
我有一個可選的二進制屬性: image
,其中包含我實體的圖像。 在界面中,我有NSImageView(圖像良好)和“刪除圖像”按鈕。 單擊圖像刪除按鈕后,我將執行以下操作:
- (IBAction)saveAction:(id)sender {
NSError *error = nil;
if (![[self managedObjectContext] save:&error]) {
[[NSApplication sharedApplication] presentError:error];
}
[tableView reloadData];
}
- (IBAction)removeImage:(id)sender {
[image setImage:nil]; // image is a NSImageView outlet bound to the image attribute.
[self saveAction:sender];
}
它從NSImageView清除圖像,但是二進制數據仍保留在Core Data實體中。
我如何在核心數據實體中反映變化?
謝謝!
編輯:
NSImageView已經綁定到模型的圖像屬性,並且也可以作為插座使用。 所以我只是在找人告訴我如何通過獲取模型來重置屬性(如果那是我需要做的)。
希望得到任何代碼幫助。 :)
[image setImage:nil];
image
實際上是圖像視圖嗎? 如果是這樣,我必須提醒您清楚,准確地命名實例變量。
您需要設置模型對象的image
屬性,而不是視圖。 通過控制器將視圖綁定到模型; 然后,當您更改模型時,視圖將免費收取更改。
我的印象是,從獲取請求中更改數組不會對存儲中的實際數據產生影響。 但是我錯了。 我嘗試了並且成功了! 謝謝彼得,以及其他所有人!
這是我為具有唯一屬性的當前選定實體替換的圖像去除功能的內容:
- (IBAction)removeImage:(id)sender {
// Fetch the entity in question.
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObjectModel *model = [self managedObjectModel];
NSEntityDescription *entity = [[model entitiesByName] valueForKey:@"myEntity"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"unique_attr == %@", [unique_attr_outlet stringValue]];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:entity];
[fetch setPredicate:predicate];
// Load it into NSArray object and remove the binary data attribute.
NSArray *contextArray = [context executeFetchRequest:fetch error:nil];
if ([contextArray count] > 0)
[[contextArray objectAtIndex:0] setValue:nil forKey:@"myImage"];
[fetch release];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.