2017-05-19 9 views
0

Core Data Bookmark IDとJSON Web API IDを一致させることができず、JSON APIからCore Dataに画像を保存できません。コアデータIDとWeb JSON API IDが一致しませんか?

Mainclass

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) 
    } 

私のコードが間違っている理由を私は理解することはできません。

答えて

0

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 
} 
+0

私も、このいずれかを試してみるが、私はそのすべての値は、あなたが修正を試してみました前から –

+0

あなたが保存されている可能性のあるデータを複製保存一致し得ていないのです。アプリを消去して再インストールしてください。これからは、新しいIDを持っている場合にのみ新しいブックマークを保存します。 – pesch

+0

私はそれを試しましたが、コアデータのブックマークエンティティ –

関連する問題