如何在核心数据swift 3中同时保存和获取
Posted
技术标签:
【中文标题】如何在核心数据swift 3中同时保存和获取【英文标题】:how to save and fetch at the same time in coredata swift3 【发布时间】:2017-11-17 19:01:37 【问题描述】:在我当前的项目中,我将(图像属性)之类的数据保存到 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 []
【问题讨论】:
在获取 .你是写完后在代码里做的吗? 写入核心数据后,我只是在获取 写我的意思是你保存你的上下文吗? 请检查我的代码。如果我错了,请纠正我。 在延迟 0.6 秒后尝试调用fetchRecordsWithClassName
并检查
【参考方案1】:
最后,它通过使用以下代码并使用父子上下文(例如在后台线程中创建私有managedObjectContext
)来工作。
func saveImageDetailsFromArray(dictionary : Dictionary<String, Any>,descriptiondictionary : Dictionary<String, Any>?, selectedImages: Array<PHAsset>, 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)")
【讨论】:
【参考方案2】:视图控制器--1: =====================>
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:
=====================>
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)
【讨论】:
虽然这可能会回答这个问题,但最好解释一下答案的基本部分,以及 OPs 代码可能存在的问题。另外:注意格式化您的帖子 谢谢@pirho。已注明,下次我将照此办理。以上是关于如何在核心数据swift 3中同时保存和获取的主要内容,如果未能解决你的问题,请参考以下文章