2014-09-01 95 views
0

我正在开发一个使用核心数据的iOS应用程序。我正在使用Rest工具包来连接Web服务,借助其余的工具包,我正在同步数据Web服务和存储在我的本地核心数据库中。它对我来说工作得很好。当我将调用获取请求时,数据正确地得到。但一段时间后,我想从Web服务的最新更新的数据,如果我再次同步最新的数据正在获取和正确地更换在我的数据库。NSfetch请求返回相同的数据始终不返回更新的数据

但是,如果我尝试获取最新数据,则获取请求将返回保存在数据库中的第一个数据。但是,如果我重新启动我的应用程序的获取请求返回更新的数据。什么是实际问题。我是iOS新手,所以我不明白这个问题。我将如何解决这个问题。

+2

请发布您的代码。 – 2014-09-01 06:56:59

+0

@Timuçin哪个代码获取请求代码?或数据映射代码? – user3762713 2014-09-01 07:10:20

+0

您是使用fetchedResultsController还是仅使用fetchRequest? – 2014-09-01 08:14:39

回答

0

NSFetchedResultsController

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController; 


- (void)viewDidLoad 
{ 
    [sef fetchAll]; 
} 


// initialize and fetch all data and also implements it's delgate 

#pragma mark Fetch All Data from core data 
-(void)fetchAll 
{ 
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"SongInfo"]; 
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"songTitle" ascending:YES]; 
    fetchRequest.sortDescriptors = @[descriptor]; 
    NSError *error = nil; 

    // Setup fetched results 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                    managedObjectContext:  [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext 
                     sectionNameKeyPath:nil 
                       cacheName:nil]; 
    [self.fetchedResultsController setDelegate:self]; 
    BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error]; 
    // NSAssert([[self.fetchedResultsController fetchedObjects] count], @"Seeding didn't work..."); 
    if (! fetchSuccessful) { 
    // RKTwitterShowAlertWithError(error); 
    } 

    if ([[self.fetchedResultsController fetchedObjects]count ]==0) { 
     // reload your table 
    } 
    else 
    { 

    } 

} 


    #pragma mark - NSFetchedResultsControllerDelegate 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
    { 
    [tbl relodaData]; 
    // reload your view here 
    } 
+0

我在很多地方使用获取请求。实际上nsfetch请求和nsfetch请求控制器有什么区别? – user3762713 2014-09-01 07:38:07

+0

获取的结果控制器委托将在内容将被更改时得到通知,使委托人有机会在表视图中更改动画 – Kalpesh 2014-09-01 08:42:39

0

一次尝试确保您与REST服务同步的本地数据库之后正确调用contextsave方法。

如果您使用fetchedResultsController,则可能由于缓存数据而发生此错误。尝试清除缓存的数据以获取正确更新的数据,

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
    return _fetchedResultsController; 
    } 

    // Clear cache 
    [NSFetchedResultsController deleteCacheWithName:@"Root"]; 

    // Your codes 

} 
+0

我没有使用fetchedResultsController.Just只有获取请求。 – user3762713 2014-09-01 08:44:59

+0

首先,我在应用程序委托中编写了上下文保存功能,但现在将上下文保存功能添加到了我的视图控制器中,并且再次检查了没有更改 – user3762713 2014-09-01 08:53:03

+0

@ user3762713如果您使用的是REST套件,则服务中的数据将使用“mainQueueManagedObjectContext” RKManagedObjectStore'。对?你是否使用相同的上下文来获取数据? – 2014-09-01 08:59:45