【问题标题】:Core data id and Web JSON api id does not match?核心数据 id 和 Web JSON api id 不匹配?
【发布时间】:2017-10-19 03:52:42
【问题描述】:

我无法匹配 Core Data 书签 ID 和 JSON Web API ID,我无法将图像从 JSON API 保存到 Core Data。

主类

import UIKit
import MapKit
import CoreLocation
import SDWebImage
import CoreData
class InfoViewController: UIViewController  , MKMapViewDelegate,CLLocationManagerDelegate{
 var get_details : schools?
 var bookmark : [Bookmark] = []

 var bookmark_details : Bookmark?


 override func viewDidLoad() {
        super.viewDidLoad()
}

        func bookmarktoCoredata(){
        let alert = UIAlertController(title: "Add BookMark", message: "", preferredStyle: .alert)
        let add = UIAlertAction(title: "Add", style: .default){
            (action) in

//            for bkms in self.bookmark
//            {
//                if ((bkms.name)) == ((self.get_details?.name)!){
//                   
//                    print("same name")
//
//                }else{
//                     print("not same")
//                }
//            }

            let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext


            let task = Bookmark(context: context)

            if (task.bookmark_id) == Int16((self.get_details?.schoolid)!){
                print("same id")
            }
            else
            {
                print("not same id")
                task.bookmark_id = Int16((self.get_details?.schoolid)!)

                task.name = self.get_details?.name
                task.address = self.get_details?.address
                task.district = self.get_details?.district
                task.country = self.get_details?.country
                task.phone = self.get_details?.phone
                task.email = self.get_details?.email
                task.website = self.get_details?.website
                task.institution = self.get_details?.institution_type
                task.establishment = self.get_details?.establishment_date
                task.admission_open = self.get_details?.admission_open_from
                task.admission_end = self.get_details?.admission_open_from
                task.latitude = (self.get_details?.latitude)!
                task.longitude = (self.get_details?.longitude)!
                task.bookmark_type = "school"

//              let img = UIImage(named: "App.png") //Sending direct image its working
                let img = UIImage(named: (self.get_details?.logo)!) //by Json it sendong nil
                let imgdata = UIImageJPEGRepresentation(img!, 1)
                task.logo = imgdata! as NSData


            (UIApplication.shared.delegate as! AppDelegate).saveContext()

            }

        }
        let cancel = UIAlertAction(title: "Cancel", style: .cancel){
            (alert) in
//            JLToast.makeText("Your Bookmark Cancel !!").show()
        }
        alert.addAction(add)
        alert.addAction(cancel)
        present(alert, animated: true, completion: nil)
    }

我不知道为什么我的代码是错误的。

【问题讨论】:

    标签: ios json core-data swift3


    【解决方案1】:

    您没有获取带有 id 的书签。试试这个:

    import UIKit
    import MapKit
    import CoreLocation
    import SDWebImage
    import CoreData
    class InfoViewController: UIViewController {
    var get_details : schools?
    var bookmark : [Bookmark] = []
    
    var bookmark_details : Bookmark?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func bookmarktoCoredata(){
        let alert = UIAlertController(title: "Add BookMark", message: "", preferredStyle: .alert)
        let add = UIAlertAction(title: "Add", style: .default) {
            (action) in
    
            let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
            let fetchId = Int16((self.get_details?.schoolid)!
            let bookmarkFetch: NSFetchRequest<Bookmark> = NSFetchRequest(entityName: "Bookmark")
            bookmarkFetch.predicate = NSPredicate(format: "bookmark_id == %d", fetchId)
            do {
                let fetchedBookmarks = try context?.fetch(bookmarkFetch)
                // you should find 1
                if fetchedBookmarks?.count == 1 {
                    print("same id")
                } else if fetchedBookmarks?.count == 0 {
                    // There is no Bookmark with this id so create a new one
                    let task = Bookmark(context: context)
                    // Populate all the properties here and save
                    task.bookmark_id = Int16((self.get_details?.schoolid)!)
    
                    task.name = self.get_details?.name
                    task.address = self.get_details?.address
                    task.district = self.get_details?.district
                    task.country = self.get_details?.country
                    task.phone = self.get_details?.phone
                    task.email = self.get_details?.email
                    task.website = self.get_details?.website
                    task.institution = self.get_details?.institution_type
                    task.establishment = self.get_details?.establishment_date
                    task.admission_open = self.get_details?.admission_open_from
                    task.admission_end = self.get_details?.admission_open_from
                    task.latitude = (self.get_details?.latitude)!
                    task.longitude = (self.get_details?.longitude)!
                    task.bookmark_type = "school"
                    // I don't know the type of get_details?.logo
                    if let imgdata = self.get_details?.logo as? NSData {
                        task.logo = UIImage(data: imgdata)
                    }
    
                    // Save your changes
                    (UIApplication.shared.delegate as! AppDelegate).saveContext()
                } else {
                    // If you get here you have more than one with the same id. Fix error
                }
            } catch {
                fatalError("Failed to fetch bookmark: \(error)")
            }
        }
        let cancel = UIAlertAction(title: "Cancel", style: .cancel){
            (alert) in
    //        JLToast.makeText("Your Bookmark Cancel !!").show()
        }
        alert.addAction(add)
        alert.addAction(cancel)
        present(alert, animated: true, completion: nil)
    }
    
    
    // MARK: - MKMapViewDelegate
    extension InfoViewController: MKMapViewDelegate {
        //Put your MKMapViewDelegate functions here
    }
    
    // MARK: - CLLocationManagerDelegate
    extension InfoViewController: CLLocationManagerDelegate {
        //Put your CLLocationManagerDelegate functions here
    }
    

    【讨论】:

    • 我也尝试过这个,但我无法匹配它保存所有重复值
    • 您可能在尝试修复之前保存了数据。删除应用程序并重新安装。从现在开始,您只有在拥有新 ID 时才会保存新书签。
    • 我试过了,但不能再次使用它在核心数据书签实体中保存重复的 id
    • get_details?.schoolid 在 Unit 中,并且 bookmark_id 在 Int16 中,所以这些 n 图像是否有问题没有保存任何解决方案,先生,谢谢!!
    • 完成了这一切,但再次在核心数据中保存重复值不知道出了什么问题,并且图像也没有保存在核心数据中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多