如何在coredata swift3中同时保存和获取

时间:2017-11-17 19:01:38

标签: ios swift core-data

在我目前的项目中,我将数据(如图像属性)保存到coredata中,但是在获取时它不会给我确切的数据数量。大部分时间提供的数据数量少于我保存的数据。但是在日志中它是显示所有数据都已成功保存。我的查询是在获取之前我可以看到日志,因此不存在数据未正确保存的问题。请帮我解决这个问题。

func saveImageDetails(aImage: UIImage?, imageUrl: String!, phAsset: PHAsset!, objectType: String, objectId: String, objectName:String,location: String?){

        if let newImage = aImage {

            let   date  = HelpherMethod.sharedInstance().toLocalTime(Date())
            let resource = PHAssetResource.assetResources(for: phAsset!).first
            let fileName = resource?.originalFilename
            var  imageName : String?
            let timestamp = date?.timeIntervalSince1970
            print(String(format:"%.0f",timestamp!))
            let currentTimeStamp = String(format:"%.0f",timestamp!)
            imageName = String(format:"%@_%@",AuthService.instance.userName,currentTimeStamp)

            let mImage = self.objectWithTypePredicate(aType: "Image", predicate: NSPredicate(format:"(imageName == %@)",imageName!), canCreate: true)

            mImage?.setValue(fileName, forKey: "fileName")
            mImage?.setValue(date, forKey: "dateTime")
            mImage?.setValue(objectId, forKey: "objectId")
            mImage?.setValue(objectType, forKey: "objectType")
            mImage?.setValue(objectName, forKey: "objectName")
            mImage?.setValue(newImage, forKey: "image")
            mImage?.setValue(imageName, forKey: "imageName")
            mImage?.setValue(imageUrl, forKey: "imageUrl")
            if let loc = location {
                mImage?.setValue(loc, forKey: "location")
            }
            mImage?.setValue("progress", forKey: "status") //Status failed for not uploaded // progress for processing// success for sync// at the initial level all status are set to progress
            do {
                try mImage?.managedObjectContext?.save()
            } catch {
                fatalError("Failure to save context: \(error)")
            }
        }
   }


//fethcing from coredata

func fetchRecordsWithClassName(entityName: String , sortingKey: String , isAscending: Bool , predicate: NSPredicate? , isAllrecord: Bool ) -> Array<Any>{


         let   managedContext = PLMCoreDataInstance.managedObjectContext
        // Initialize Fetch Request
        let fetchRequest  =  NSFetchRequest<NSFetchRequestResult>() as NSFetchRequest
        //create entity description
        let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: managedContext!)
        let sortDescriptor = NSSortDescriptor(key: sortingKey, ascending: isAscending)
        fetchRequest.sortDescriptors = [sortDescriptor]
        // optional chaining
        if let newPredicate = predicate {
            fetchRequest.predicate = newPredicate
        }
        //Configure fetch Request
        fetchRequest.entity = entityDescription
        do {
            if let results = try? managedContext?.fetch(fetchRequest)  {
                return results!
            }
        }
        return []
    }

2 个答案:

答案 0 :(得分:0)

最后,它使用下面的代码并使用父子上下文,比如在后台线程中创建私有manageobjectcontext。

func saveImageDetailsFromArray(字典:字典,descriptiondictionary:Dictionary?,selectedImages:Array,objectType:String,objectId:String,objectName:String,location:String ?, completionHandler:@escaping PLMCoreDataCompletionHandler){

    let moc = self.managedObjectContext //Our primary context on the main queue
    let privateMOC = self.backgroundContext

    for phAsset in selectedImages{

        privateMOC?.perform {

            let index = selectedImages.index(of: phAsset)
            let   date  = HelpherMethod.sharedInstance().toLocalTime(Date())
            let timestamp = date?.timeIntervalSince1970
            print(String(format:"%f",timestamp!))
            let currentTimeStamp = String(format:"%f",timestamp!)
            var  imageName : String?
            imageName = String(format:"%@_%@",AuthService.instance.userName,currentTimeStamp)
            let  newImage = dictionary[phAsset.localIdentifier]
            let resource = PHAssetResource.assetResources(for: phAsset).first
            let fileName = resource?.originalFilename
            let userName = AuthService.instance.userName
            let mImage = NSEntityDescription.insertNewObject(forEntityName: "Image", into: privateMOC!) as! Image

            mImage.setValue(fileName, forKey: "fileName")
            mImage.setValue(date, forKey: "dateTime")
            mImage.setValue(objectId, forKey: "objectId")
            mImage.setValue(objectType, forKey: "objectType")
            mImage.setValue(objectName, forKey: "objectName")
            mImage.setValue(newImage, forKey: "image")
            mImage.setValue(imageName, forKey: "imageName")
            mImage.setValue(phAsset.localIdentifier, forKey: "imageUrl")
            mImage.setValue(userName, forKey: "userName")
            mImage.setValue("progress", forKey: "status")

            if let loc = location {
                mImage.setValue(loc, forKey: "location")
            }
            if let dict  = descriptiondictionary{
                if let description = dict[phAsset.localIdentifier] {
                    mImage.setValue(description, forKey: "comment")
                }
            }

            do {
                try privateMOC?.save()
                moc.performAndWait {
                    do {
                        try moc.save()
                        print("###########save context: \(String(describing: mImage))")
                        print("###########imageName: \(String(describing: imageName)))")

                        DispatchQueue.main.async {
                            if(index == selectedImages.count - 1){
                                print("###########index: \(String(describing: index)))")
                                completionHandler(true)
                            }
                        }
                    } catch {
                        fatalError("Failure to save context: \(error)")
                    }
                }
            } catch {
                fatalError("Failure to save context: \(error)")
            }
        }
    }
}

答案 1 :(得分:-1)

的viewController - 1: ====================&GT;

import UIKit

import CoreData



class ViewController-1: UIViewController,UITableViewDelegate,UITableViewDataSource

{

    @IBOutlet var table_view: UITableView!

    var my_data_array = NSMutableArray()

    var data_array = NSMutableArray()

    var data_fet:[NSManagedObject] = []



    override func viewDidLoad()

    {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }

    override func viewWillAppear(_ animated: Bool)

    {

        let app = UIApplication.shared.delegate as! AppDelegate



        let context_view = app.persistentContainer.viewContext



        let fetch_data = NSFetchRequest<NSManagedObject>(entityName: "Student_detail")

        do

        {

//            



            data_array = try context_view.fetch(fetch_data as! NSFetchRequest<NSFetchRequestResult>) as! NSMutableArray

            print("this is array",data_array)

        }

        catch let error as NSError

        {

            print(error)

        }

        table_view.reloadData()

    }

    func numberOfSections(in tableView: UITableView) -> Int

    {

        return 1

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    {

        return data_array.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    {

        let t_cell = tableView.dequeueReusableCell(withIdentifier: "cell_data", for: indexPath) as! TableViewCell_data_view

        let result_data = data_array[indexPath.row]

        t_cell.t_std_name.text = (result_data as AnyObject).value(forKey: "stdName") as? String

        t_cell.t_roll_no.text = (result_data as AnyObject).value(forKey: "stdRoll") as? String

        t_cell.t_std_dept.text = (result_data as AnyObject).value(forKey: "stdDept") as? String

        t_cell.t_grade.text = (result_data as AnyObject).value(forKey: "stdGrade") as? String

        t_cell.t_img_view.image = UIImage(data: (result_data as AnyObject).value(forKey: "stdImage") as! Data)



        return t_cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

    {

        performSegue(withIdentifier: "data_passing", sender: self)

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)

    {

        if segue.identifier == "data_passing"

        {

            let view_c = segue.destination as! ViewController_Register

            let index_path = table_view.indexPathForSelectedRow





            let update_std_result = self.data_array[(index_path?.row)!]

            view_c.data_result = update_std_result as! NSManagedObject

        }

    }

的viewController - 2:

=====================&GT;

import UIKit

import CoreData



class ViewController_Register: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

{

    @IBOutlet var std_name: UITextField!

    @IBOutlet var std_roll: UITextField!

    @IBOutlet var std_dept: UITextField!

    @IBOutlet var std_grade: UITextField!

    @IBOutlet var std_image: UIImageView!



    var data_result:NSManagedObject!



    override func viewDidLoad()

    {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        std_image.isUserInteractionEnabled = true

        if data_result != nil

        {

            std_name.text = data_result.value(forKey: "stdName") as? String

            std_roll.text = data_result.value(forKey: "stdRoll") as? String

            std_dept.text = data_result.value(forKey: "stdDept") as? String

            std_grade.text = data_result.value(forKey: "stdGrade") as? String

            std_image.image = UIImage(data: data_result.value(forKey: "stdImage") as! Data)

        }

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    @IBAction func image_action(_ sender: UITapGestureRecognizer)

    {

        let img_pick = UIImagePickerController()

        img_pick.delegate = self

        img_pick.allowsEditing = true

        img_pick.sourceType = UIImagePickerControllerSourceType.photoLibrary

        self.present(img_pick, animated: true)

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

    {

        if let img_delegate = info[UIImagePickerControllerOriginalImage] as? UIImage

        {

            std_image.image = img_delegate

        }

        else

        {

            print("Error")

        }

        self.dismiss(animated: true, completion: nil)

    }



    @IBAction func register_action(_ sender: Any)

    {

        let app = UIApplication.shared.delegate as! AppDelegate



        let context_view = app.persistentContainer.viewContext



        if data_result != nil

        {

            data_result.setValue(std_name.text, forKey: "stdName")

            data_result.setValue(std_roll.text, forKey: "stdRoll")

            data_result.setValue(std_dept.text, forKey: "stdDept")

            data_result.setValue(std_grade.text, forKey: "stdGrade")

            let img_data = UIImageJPEGRepresentation(std_image.image!, 1.0)!

            data_result.setValue(img_data, forKey: "stdImage")

        }

        else

        {



        let student = NSEntityDescription.insertNewObject(forEntityName: "Student_detail", into: context_view) as NSManagedObject



        student.setValue(std_name.text, forKey: "stdName")

        student.setValue(std_roll.text, forKey: "stdRoll")

        student.setValue(std_dept.text, forKey: "stdDept")

        student.setValue(std_grade.text, forKey: "stdGrade")

        let img_data = UIImageJPEGRepresentation(std_image.image!, 1.0)!

        student.setValue(img_data, forKey: "stdImage")

        do

        {

            try context_view.save()

            print("name is-->",std_name.text!,"roll no is-->",std_roll.text!,"dept is -->",std_dept.text!)

        }

        catch let error as NSError

        {

            print(error.localizedDescription)

        }

    }

        self.navigationController?.popViewController(animated: true)



    }


}