2014-09-01 86 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