ios - 未调用 NSFectchedResultController 委托(delegate)?
<p><p>我在我的应用程序中使用 <code>NSFetchedResultController</code> 和 <code>core data</code>。但是当我插入新记录时,不会调用 <code>NSFetchedResultController 委托(delegate)</code> 方法,但是当我尝试删除或更新现有记录时它会起作用。这是我的代码 - </p>
<pre><code>- (void)viewDidLoad
{
;
// Do any additional setup after loading the view.
//fetch friends from DB
AppDelegate* appDelegate = .delegate;
self.managedObjectContext = appDelegate.managedObjectContext;
NSError *error;
if (![ performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, );
exit(-1);// Fail
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [ init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Friends" inManagedObjectContext:self.managedObjectContext];
;
;
NSPredicate *predicate = ;
;
NSSortDescriptor *sort = [
initWithKey:@"userId" ascending:NO];
];
NSFetchedResultsController *theFetchedResultsController =
[ initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
</code></pre>
<p>这个用于保存/更新/删除 -</p>
<pre><code>-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 1001) {
if (buttonIndex == 0) { // add new friend
NSString* fid = [.text stringByTrimmingCharactersInSet:];
if (fid.length == 0) {
;
return;
}
NSString* userid = [ valueForKey:@"userId"];
NSString* req = ;
if () {
;
return;
}
[ConnectionManager invokeAsync:@"GET" body:req request:@"/friends" viewForHud:self.view completion:^(NSDictionary* res, NSString* error){
if (error) {
UIAlertView* alert = [ initWithTitle:@"Error!" message:error delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
;
}
else
{
Friends * newEntry = ;
newEntry.userId = userid;
newEntry.friendId = fid;
newEntry.status = @"0";
NSError *error;
if (!) {
NSLog(@"Whoops, couldn't save: %@", );
}
//;
//;
UIAlertView* alert = [ initWithTitle:@"Success!" message: delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
;
}
}];
}
}
else if (alertView.tag == 1002){ // delete friend
NSManagedObject *managedObject = ;
;
;
;
UIAlertView * alert = [ initWithTitle:@"Success!" message:@"Friend removed from your friend list." delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:Nil, nil];
;
}
else if (alertView.tag == 1003){ // update friend
NSString* req = ;
[ConnectionManager invokeAsync:@"GET" body:req request:@"/friends" viewForHud:self.view completion:^(NSDictionary* res, NSString* error){
if (error) {
UIAlertView* alert = [ initWithTitle:@"Error!" message:error delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
;
}
else
{
NSString* userid = [ valueForKey:@"userId"];
Friends* newFriend;
NSError * error;
NSFetchRequest * fetchRequest = [ init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Friends"
inManagedObjectContext:managedObjectContext]];
;
];
if ()
newFriend = [ lastObject];
newFriend.status = @"1";
// save
if (! )
NSLog(@"Couldn't save data to %@", NSStringFromClass());
;
UIAlertView * alert = [ initWithTitle:@"Success!" message:@"Friend blocked from your friend list." delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:Nil, nil];
;
}
}];
}
}
</code></pre>
<p>数据已保存到数据库但未重新加载 TableView 。我做错了什么?任何建议将不胜感激。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我猜是问题</p>
<pre><code>NSPredicate *predicate = ;
</code></pre>
<p>用 </p> 改变它
<pre><code>NSPredicate *predicate = ;
</code></pre>
<p>希望这会有所帮助..</p></p>
<p style="font-size: 20px;">关于ios - 未调用 NSFectchedResultController 委托(delegate)?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22688757/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22688757/
</a>
</p>
页:
[1]