prepareForSegue清空我的NSManagedObject数组

时间:2016-05-26 02:08:37

标签: ios arrays swift core-data nsmanagedobject

我需要在当前应用程序中将数据从一个视图传递到另一个视图我正在使用最新版本的Swift for iOS 8.我使用一个函数来填充FacultyArray,该函数使用MagicalRecord从CoreData获取信息。我遇到的问题是,当我尝试在prepareForSegue函数中使用填充数组时,它变为空。

import UIKit
import SwiftyJSON


class EspecialidadTVC: UITableViewController {

let defaults = NSUserDefaults.standardUserDefaults()
let endpoint = Connection()

var facultyList:Array<Faculty>? = TR_Faculty().get()

override func viewDidLoad() {
    super.viewDidLoad()

}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    //return self.facultyArray!.count
    return (self.facultyList?.count)!
}


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

    cell.textLabel?.text = self.facultyList![indexPath.row].nombre
    return cell
}

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/


// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "presentMainMenuSegue" {
        let splitViewController:MasterSVC = segue.destinationViewController as! MasterSVC

        let indexpath = self.tableView.indexPathForSelectedRow


        splitViewController.faculty = self.facultyList![indexpath!.row]

        //splitViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
    }

    if segue.identifier == "logoutSegue" {

    }
}

}

是否有更好的方法在视图之间发送NSManagedObject?

更新

这是MasterSVC的目标代码。没有控制台错误,只是我的数组被清空了,所以当我发送对象时,这是一个零对象。

import UIKit
import Foundation

class MasterSVC: UISplitViewController {

var faculty:Faculty!

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
    navigationItem.leftItemsSupplementBackButton = true
}

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


// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

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

}

}

更新2

填充数组的方法

class TR_Faculty {

let dateFormatter = NSDateFormatter()

func store(json: JSON) {

    //Clear previous data
    Faculty.MR_truncateAll()

    for (index,subJson):(String, JSON) in json {
        let fac = Faculty.MR_createEntity()
        fac?.id = Int(subJson["IdEspecialidad"].stringValue)
        fac?.codigo = subJson["Codigo"].stringValue
        fac?.nombre = subJson["Nombre"].stringValue
        fac?.descripcion = subJson["Descripcion"].stringValue
        fac?.updated_at = self.dateFormatter.dateFromString(subJson["updated_at"].stringValue)
    }

    NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()
}

func get()-> Array<Faculty>?{
    return Faculty.MR_findAll() as! Array<Faculty>
}

}

1 个答案:

答案 0 :(得分:0)

我遇到的问题是对服务的调用是异步的,所以在执行到另一个视图控制器的segue之前,没有什么能保证调用完成。对我有用的只是将performSegueWithIdentifier置于通话的成功部分内。

作为其他人开始在移动应用程序中使用API​​的提示,当您想从应用程序调用restful API时,您应该进行调用,等待它成功完成然后填充SQLite数据库。之后,您可以调用数据库来填充视图。