2016-04-29 190 views
1

我寫了兩個分開的函數fetchRecordsdisplayRecords寫入Appdelegate類。 fetchRecords函數將從實體中提取所有記錄,並且它工作正常。 displayRecords函數接受來自fetchRecords函數的返回值,並逐個打印所有記錄。

我有一個視圖控制器,它調用這兩個函數來完成所需的任務。我的問題是result.countdisplayRecords顯示提取記錄中可用記錄的總數。在逐一打印記錄時,值爲零。核心數據 - NSManagedObject返回無

override func viewDidLoad() { 
super.viewDidLoad() 
    let fetchedRecords = AppDelegate().fetchRecords(fromEntity: "Dashboard") 
    AppDelegate().displayRecords(fromFetchedRecords: fetchedRecords) 
} 

這裏是寫在AppDelegate類

func fetchRecords(fromEntity entity:String) -> Array<AnyObject> { 
    var fetchedResult:Array<AnyObject> = [] 
    let fetchRequest = NSFetchRequest() 
    let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: self.managedObjectContext)! 
    fetchRequest.entity = entityDescription 
    do{ 
     fetchedResult = try self.managedObjectContext.executeFetchRequest(fetchRequest)    
    }catch{ 
     let fetchError = error as NSError? 
     print(fetchError!) 
    } 
    return fetchedResult 
} 

func displayRecords(fromFetchedRecords result:Array<AnyObject>) { 
    print("Total records:\(result.count)") 
    if (result.count > 0) { 
     for data in result { 
     let dashboard = data as! NSManagedObject 
     print("Value: \(dashboard.valueForKey("count"))") 
     } 
    } 
} 

fetchRecordsdisplayRecords功能添加我的數據模型這裏 enter image description here

我也將分享數據插入代碼。

func saveDashBoardData(dictionary: Dictionary<String, String>) { 
    print(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)) 

    //Create Manage Object 
    let entityDescription: NSEntityDescription = NSEntityDescription.entityForName("Dashboard", inManagedObjectContext: self.managedObjectContext)! 
    for data in dictionary { 
     let dashboardObject = Dashboard(entity: entityDescription,insertIntoManagedObjectContext: self.managedObjectContext) 
     dashboardObject.type = data.0 
     dashboardObject.count = data.1 
     do { 
      try self.managedObjectContext.save() 
      print("Data saved succesfully") 
     } 
     catch { 
      print(error) 
     } 
    } 
} 
+0

你能告訴我你的coredata模型項目@sindhu JA –

+0

我有共同的問題的數據模型。請找到它 –

+0

我無法得到你想說的話@vadian –

回答

3

問題是AppDelegate()總是創建一個類的新實例,該實例與應用程序的「硬編碼」委託實例不同。

你必須寫

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
let fetchedRecords = appDelegate.fetchRecords(fromEntity: "Dashboard") 
appDelegate.displayRecords(fromFetchedRecords: fetchedRecords) 

既然你已經創建的NSManagedObject自定義子類使用它作爲類型,而不是NSManagedObject或 - 更糟 - 未指定AnyObject。這使得更容易很多事情:

func fetchRecords(fromEntity entity:String) -> [Dashboard] { 
    let fetchRequest = NSFetchRequest() 
    let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: self.managedObjectContext)! 
    fetchRequest.entity = entityDescription 
    do { 
    return try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [Dashboard]    
    } catch let fetchError as NSError { 
    print(fetchError!) 
    } 
    return [Dashboard]() 
} 

func displayRecords(fromFetchedRecords result:[Dashboard]) { 
    print("Total records:\(result.count)") 

    for dashboard in result { // the check for empty is not needed 
     print("Value: \(dashboard.count)") 
    } 
} 
+0

謝謝@vadian。爲AppDelegate創建實例解決了這個問題。 –

+0

使用與AppDelegate的緊密耦合是不好的,會給你帶來更多的麻煩。你最好遵循最近在[核心數據編程指南](https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/CoreData/index.html)中設計的設計,而不是什麼是在模板中。 –

0

試試這個..

let dashboard = Dashboard as! NSManagedObject 

print("Value: \(dashboard.count)") 

我認爲它可能工作。因爲您已將您的ManagedObject作爲數據。最好你把它看作儀表板。所以,你可以通過dashboardObj.count訪問count ..

希望你爲實體創建了Core Data NSManagedSubclass。

希望它可以幫助..

+0

是的,我已經爲該實體創建了NSManagedSubclass。我試過你的建議,但它也不起作用。 –

0

從coredata模型創建儀表板的NSManagedObject子類。

func displayRecords(fromFetchedRecords result:Array<AnyObject>) { 

    print("Total records:\(result.count)") 
    if (result.count > 0) { 
     for data in result { 
     let dashboard = data as! Dashboard 
     print("Value: \(dashboard.valueForKey("count"))") 
     } 
    } 
} 

這裏的關鍵變化是讓dashboard = data as!儀表板。 儀表板是您需要使用核心數據模型幫助創建的管理對象子類。

+0

謝謝@Dovakin。我們使用你的建議併爲'AppDelegate'創建實例來解決問題。 –