2016-12-06 70 views
0

我在編輯單元格後從核心數據持久存儲中重新加載數據時遇到問題。如何在cellEdit後強制從持久性存儲重新加載核心數據

我試着重新加載tableView本身,但不強制從持久存儲中獲取,我如何強制重新加載?

這是我在重裝嘗試不工作,如果我退出的UIViewController和回去的所有作品,所以我知道持久存儲正確更新

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 

    NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; 

    if([NWTillHelper isDebug] == 1) { 
     NSLog(@"TransactionListView:tableView:context = %@", context); 
    } 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     NSManagedObject *transactionRow = [self.transactionRowsRows objectAtIndex:indexPath.row]; 

     [transactionRow setValue:[NSNumber numberWithInt:[@"999" intValue]] forKey:@"status"]; 
     [transactionRow setValue:[NSNumber numberWithInt:[@"0" intValue]] forKey:@"qty"]; 
     [transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"price"]; 
     [transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"discountPercentage"]; 
     [transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"discountAmount"]; 
     [transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"amountTax"]; 
     [transactionRow setValue:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"sum"]; 

     NSError *error = nil; 
     if (![context save:&error]) { 
      if([NWTillHelper isDebug] == 1) { 
       NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
       return; 
      } 
     } 
     [self.transactionListViewTbl reloadData]; 
    } 
} 

回答

1

只是想提到這一點。

在閱讀完您的問題之後,我聽起來並不覺得它會使用Fetch Controller進行修復。 Fetch Controller非常整潔,它可以讓你更新表而不需要顯式調用[table reloadData]。但是,如果重新加載表的數據沒有更新,可能有更多的託管對象上下文更新您的持久存儲。

數據已被不同的託管對象上下文更新,並且您沒有獲取連接表的那個更新。我首先看看我的核心數據堆棧和受管對象上下文的數量。如果您需要超過1個,您可以訂閱更改,或者確保您想要查看的更改在同一個託管對象上下文中完成。

希望它有幫助。

Managed Object Context - Apple Docs

+0

我有一個託管的對象上下文在每個方法,但在同一個方法,我只有一個,它可以在方法之間衝突嗎?如果是這樣的話,我怎樣才能製造一個貫穿所有方法的堅持? –

+1

謝謝,我有兩個上下文競爭我是一個複製和粘貼錯誤,許多深夜編碼我認爲,欣賞這個頭。 –

1

。利用NSFetchedResultsController的作爲數據源並實施NSFecthedResultsController代表

您可以創建NSFetchedResults控制器在你的viewController的屬性,創建它的一個實例

lazy var transactionFetchedResultsController : NSFetchedResultsController<Transaction> = { 
     let appdelegate = UIApplication.shared.delegate as! AppDelegate 
     var fetchedResultsController : NSFetchedResultsController<Transaction>! 

     appdelegate.managedObjectContext.performAndWait { 
      var fetchRequest: NSFetchRequest<Transaction> 
      if #available(iOS 10.0, *) { 
       fetchRequest = Transaction.fetchRequest() 
      } else { 
       fetchRequest = NSFetchRequest(entityName: "Transaction") 
      } 

      fetchRequest.sortDescriptors = [NSSortDescriptor(key: "your_property", ascending: true)] 
      let yourPredicate = NSPredicate(format: "your condition == whatever") 
      fetchRequest.predicate = yourPredicate 
      fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: appdelegate.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) 
     } 
     return fetchedResultsController 
    }() 

集委託給你的viewController

self. transactionFetchedResultsController.delegate = self 

最後初始化它在viewDidLoad中()

try! self. transactionFetchedResultsController.performFetch() 

現在實行代表:)

extension YourViewController : NSFetchedResultsControllerDelegate{ 
    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 
     //row updated 
    } 

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) { 
     //section updated 
    } 

    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
     //change completed 
     //reload your tableView 
    } 
} 

編輯

如果CAS e你想知道如何使用NSFetchedResultsController作爲tableView數據源?

extension YourViewController: UITableViewDataSource { 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return (self. transactionFetchedResultsController.sections?.count)! 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      //assuming you have only one section 
      //you can always return number of items in each section using NSFetchedResultsController :) 
      return (self. transactionFetchedResultsController.fetchedObjects?.count)! 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     //let cell = .... 
     //you can instantiate your cell here 
     //you can access your transaction as below 
     let transaction = self. transactionFetchedResultsController.fetchedObjects![indexPath.row] 
     //return your cell 
     return cell 
    } 
} 

希望它能幫助:)

+1

這看起來不錯,我想轉換爲ObjC –

+0

@亞光douhan:對不起,哥們不具有客觀的C代碼示例停止它的工作從相當一段時間,現在:( –

+1

你把我不用擔心在正確的道路上,我可以RTFM –

相關問題