2015-08-21 3 views
0

Я пытаюсь сделать приложение для заметок, все работает правильно, но после сохранения примечания, но вид таблицы пуст.Попытка сделать приложение заметок с разбором

Все заметки, которые я написал, всплывают в течение секунды и исчезают, и заметки появляются в базе данных синтаксического анализа.

// 
// MasterTableViewController.swift 
// Notes 
// 
// Created by uLife on 6/17/15. 
// Copyright (c) 2015 uLife. All rights reserved. 


import UIKit 

class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate { 

var noteObjects: NSMutableArray! = NSMutableArray() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    if (PFUser.currentUser() == nil) { 



    }else { 

     self.fetchAllObjectsFromLocalDatastore() 
     self.fetchAllObjects() 

    } 

} 

func fetchAllObjectsFromLocalDatastore() { 

    var query: PFQuery = PFQuery(className: "Note") 

    query.fromLocalDatastore() 

    query.whereKey("username", equalTo: PFUser.currentUser()!.username!) 

    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 

     if (error == nil) { 

      var temp: NSArray = objects!; NSArray.self 

      self.noteObjects = temp.mutableCopy() as! NSMutableArray 

      self.tableView.reloadData() 

     }else { 

      println(error!.userInfo) 

     } 

    } 

} 

func fetchAllObjects() { 

    PFObject.unpinAllObjectsInBackgroundWithBlock(nil) 

    var query: PFQuery = PFQuery(className: "Note") 

    query.whereKey("username", equalTo: PFUser.currentUser()!.username!) 

    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 

     if (error == nil) { 

      PFObject.pinAllInBackground(objects, block: nil) 

      self.fetchAllObjectsFromLocalDatastore() 

     }else { 

      println(error!.userInfo) 

     } 

    } 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Parse Login 

    // MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return self.noteObjects.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MasterTableViewCell 


    var object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject 

    cell.masterTitleLabel?.text = object["title"] as? String 
    cell.masterTextLabel?.text = object["text"] as? String 

    return cell 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    self.performSegueWithIdentifier("editNote", sender: self) 

} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 


    var upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController 

    if (segue.identifier == "editNote") { 

     let indexPath = self.tableView.indexPathForSelectedRow()! 

     var object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject 

     upcoming.object = object 

     self.tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    } 

} 

}

+0

Вы можете публиковать скриншоты своего класса синтаксического анализа? а также код работает нормально? – Lamar

ответ

0

Я думаю, ваша проблема здесь. pinAllInBackground - это асинхронный метод, поэтому fetchAllObjectsFromLocalDatastore будет выполнен немедленно, и, очевидно, в то время в локальном хранилище данных не будет объектов.

PFObject.pinAllInBackground(objects, block: nil) 

self.fetchAllObjectsFromLocalDatastore() 

Так что вам нужно делать? Внедрение PFBooleanResultBlock.

PFObject.pinAllInBackground(objects, block: { (success, error) in 
    if error == nil { 
    self.fetchAllObjectsFromLocalDatastore() 
    } 
}) 
+0

Большое вам спасибо! –

Смежные вопросы