2016-09-04 67 views
0

我對編碼領域相當陌生,我試圖用Firebase實現tableView。我試圖實現是:Firebase + Swift:無法刪除tableView中的行

  1. 使用的tableView,用戶可以輸入自己喜歡的位置
  2. 創建窗體(我用尤里卡這裏)供用戶挑選從某些選項創建一個「設置」頁面他們在設置頁面中輸入您的收藏夾
  3. 允許刪除那些「收藏夾」

爲了形式的工作,我拉在AppDelegate中通過user.uid過濾火力地堡數據。填充這些'收藏夾設置'。當我嘗試在'設置'處使用commitEdittingStyle刪除行時,它會拋出無效行數的錯誤。我嘗試搜索各種解決方案,並嘗試dispatch_async,但它不知何故只是不完全解決我的問題。我的一些代碼片段如下:

public var setUpFavLocations: [(String)] = [] 
public var favLocDatabase: [FIRDataSnapshot]! = [] 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 
var ref: FIRDatabaseReference! 
var _refHandle: FIRDatabaseHandle! 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    FIRApp.configure() 

    let favLocVC: UITableViewController = FavLocationsTableViewController(style: UITableViewStyle.Plain) 


    ref = FIRDatabase.database().reference() 

    if let user = FIRAuth.auth()?.currentUser { 

     self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in 
      favLocDatabase.append(snapshot) 
      let insertionIndexPath = NSIndexPath(forRow: favLocDatabase.count - 1, inSection: 0) 
      favLocVC.tableView.insertRowsAtIndexPaths([insertionIndexPath], withRowAnimation: .Automatic) 
     }) 

     self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in 
      setUpFavLocations.append(snapshot.value?["favLocation"] as! String) 
     }) 

//At FavLocationsTableViewController, aka at the settings page 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print("count \(setUpFavLocations.count)") 
    return setUpFavLocations.count 
} 

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

    if editingStyle == .Delete { 

     if let user = FIRAuth.auth()?.currentUser { 
      let row = favLocDatabase[indexPath.row] 

      self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildRemoved, withBlock: { (snapshot) -> Void in 
        favLocDatabase.removeAtIndex(indexPath.row) 
        self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: .Automatic) 

      }) 
      row.ref.removeValue() 
     } 

    } 
} 

這樣做,這將引發我下面的錯誤

終止應用程序由於未捕獲的異常
「NSInternalInconsistencyException」,理由是:「無效的更新:無效 數(5)中的一個 現有節中包含的行數必須等於更新前(5)加上或減去 插入的行數或者d從該部分選擇(0插入, 1刪除)並加上或減去移入或移出 該部分(移入0,移出0)的行數。

請幫忙,我在這裏呆了好幾天。非常感激。

+0

等等,你不是說你使用尤里卡?爲什麼不在這裏使用Eureka?這很容易! – Sweeper

+0

@Sweeper我無法找到最適合此目的的Eureka單元。 – Koh

+0

難道是你從數據庫中刪除了兩次該項目('removeAtIndex'和'removeValue')?爲什麼你創建一個新的索引路徑,而不是僅僅使用參數中傳遞的(實際上相同的)路徑? – vadian

回答

0

您每次刪除一行時都會創建一個新的偵聽器,這不會很好。

嘗試移動:

self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildRemoved, withBlock: { (snapshot) -> Void in 
    favLocDatabase.removeAtIndex(indexPath.row) 
    self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: .Automatic) 
}) 

與其他聽衆AppDelegate中。

另外我注意到你沒有處理你的觀察者,所以當你打開和關閉你的應用程序時,觀察者將會重複。您應該在應用程序進入後臺時刪除觀察者,並在應用程序進入前臺時重新實例化它們。你會在你的AppDelegate中找到這些事件的方法。

+0

如何獲取AppDelegate的indexPath? – Koh