我有一些对象是与对象用户mani-to-one相关的图像,例如Image <<---> User。现在我想做,当用户登录时,我会为每个图像显示一个按钮以添加到收藏夹,当我单击此按钮时运行此代码:
User * user = [[UserController sharedInstance] currentUser];
Image * image = (Image*)[user.managedObjectContext objectWithID:[self.yacht objectID]];
yacht.whoLiked = user
问题不在于我是同一个控制器,而是之前在主控制器中,因为我所做的是在集合视图中加载图像的拇指(并在此控制器中加载数据库中的所有数据)然后当我按下拇指时我走了在另一个向我显示大图像和添加收藏夹按钮的控制器中,当我按下它然后回到旧控制器的 viewDidAppear 中的旧控制器时,我每次从数据库中重新加载数据但我看不到任何更改,如果我更改部分(控制器)并且我回来查看我看到的数据更新
这就是我从主控制器调用 Db 的方式:
- (void)fetchImages
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Image"];
request.predicate = [NSPredicate predicateWithFormat:@"ANY whichCategories.name =[cd] %@", self.category.name];
NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"headline" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
request.sortDescriptors = [NSArray arrayWithObject:sortName];
NSError * error = nil;
self.images = [self.database.managedObjectContext executeFetchRequest:request error:&error];
[self.collectionView reloadData];
}
- (void)useDocument
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.database.fileURL path]]) {
// CREATE
[self.database saveToURL:self.database.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
[self fetchImages];
}];
} else if (self.database.documentState == UIDocumentStateClosed) {
// OPEN
[self.database openWithCompletionHandler:^(BOOL success) {
[self fetchImages];
}];
} else if (self.database.documentState == UIDocumentStateNormal) {
// USE
[self fetchImages];
}
}
- (void)setDatabase:(UIManagedDocument *)database
{
if (_database != database) {
_database = database;
[self useDocument];
}
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self useDocument];
//[self.collectionView reloadData];
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.5];
self.collectionView.alpha = 1;
[UIView commitAnimations];
}
为什么如果我回来并返回代码工作就像我没有调用服务器来刷新数组一样?