2017-07-18 55 views

回答

0

從iOS 11和Xcode 9開始,Core Data支持URI數據類型,該類型適合存儲來自JSON的profimg字段的圖像URL。你也可以使用核心數據的字符串類型,爲了方便起見,或者如果你支持iOS 10. UID字段是一個整數,所以選擇任何一個核心數據的整數類型足夠大,以保持最大值。如果您對最大UID有疑問,可以安全地使用Core Data的「Integer 64」類型。

保存和提取就像任何其他核心數據屬性。

+0

你好Tom,請參閱第二個答案,我像這樣做了這個問題。但問題是當我提取數據並在表格視圖中顯示時,如果沒有互聯網連接,應用程序崩潰 –

+0

這是一個與此完全不同的問題。您應該發佈一個新問題,其中包括有關您如何使用網絡連接的信息以及應用程序崩潰時發生的情況 - 錯誤消息等。 –

-1

FUNC clearData(){

let delegate = UIApplication.shared.delegate as? AppDelegate 

    if let context = delegate?.persistentContainer.viewContext{ 


     do { 




      let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Mydata") 

      let objects = try(context.fetch(fetchRequest)) as? [NSManagedObject] 




      for object in objects! { 
       context.delete(object) 

      } 

      try(context.save()) 


     } catch let err { 
      print(err) 
     } 
    } 


} 


func setupData() { 

     clearData() 

    let urlWithParams = "https://www.zdoof.com/api/zTimeline/ztimeline_bind?Created_by=4490&Created_on=01/jan/2017&Updated_on=14/june/2017&PageIndex=1&PageSize=50" 

    Alamofire.request(urlWithParams, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in 

     switch(response.result) { 
     case .success(_): 
      if let data = response.result.value as? NSArray{ 
       self.myHelp = [myHelperClass]() 





       for allData in data { 

        let help = myHelperClass() 
        let jsonID = (allData as! NSDictionary).value(forKey: "UID") as! NSInteger 
        help.myId = String.init(describing: jsonID) 

        help.myName = ((allData as! NSDictionary).value(forKey: "Name") as! NSString) as String 

        self.myNameArray.append(help.myName) 

        //       person.setValue(myName, forKey: "name") 


        DispatchQueue.main.async(execute: {() -> Void in 


         let myUrlString = "https://www.zdoof.com//" 

         let myprofileImage = (allData as! NSDictionary).value(forKey: "profimg") as! NSString 
         help.myStringImageArray = myUrlString + (myprofileImage as String) 




         let midImage = ((allData as! NSDictionary).value(forKey: "mid_img") as! NSString) as String 

         help.myStringPostImageArray = myUrlString + (midImage as String) 




        }) 





        help.Yumms = ((allData as AnyObject).value(forKey: "Postlike") as! NSString) as String 


        let kUserDefault = UserDefaults.standard 



        //       person.setValue(Yumms, forKeyPath: "postlike") 

        let myId = (allData as AnyObject).value(forKey: "UID") as!NSInteger 
        help.myidString = String.init(describing: myId) 
        //       person.setValue(myidString, forKey: "uID") 


        help.Rebcol = ((allData as AnyObject).value(forKey: "Rebcol") as! NSString) as String 
        //       person.setValue(Rebcol, forKey: "rebcol") 


        help.UTYPIMG = ((allData as AnyObject).value(forKey: "UTYPIMG") as! NSString) as String 
        //       person.setValue(UTYPIMG, forKey: "uTYPIMG") 

        help.Created_on = ((allData as AnyObject).value(forKey: "Created_on") as! NSString) as String 
        //       person.setValue(Created_on, forKey: "created_on") 

        help.UTYP = ((allData as AnyObject).value(forKey: "UTYP") as! NSString) as String 
        //       person.setValue(UTYP, forKey: "uTYP") 

        help.comments = ((allData as AnyObject).value(forKey: "Postcomment") as! NSString) as String 

        //       person.setValue(comments, forKeyPath: "postcomment") 

        help.Details = ((allData as AnyObject).value(forKey: "Details") as! NSString) as String 


        //       person.setValue(Details, forKeyPath: "details") 
        help.myPost = ((allData as AnyObject).value(forKey: "Share") as! NSString) as String 


        //       person.setValue(myPost, forKeyPath: "share") 

        help.myDate = ((allData as AnyObject).value(forKey: "zDate") as! NSString) as String 


        //       person.setValue(myDate, forKeyPath: "zDate") 


        let myPTYPEInteger = (allData as AnyObject).value(forKey: "PTYPE") as!NSInteger 

        help.myPTYPE = String.init(describing: myPTYPEInteger) 
        let myIdInteger = (allData as AnyObject).value(forKey: "ID") as!NSInteger 
        help.myId = String.init(describing: myIdInteger) 


        self.myFunction(nameFunc: help.myName, yummFunc: help.Yumms, myIdFunc: help.myidString, RebcolFunc: help.Rebcol, UTYPIMGFunc: help.UTYPIMG, Created_onFunc: help.Created_on, UTYPFunc: help.UTYP, commentsFunc: help.comments, DetailsFunc: help.Details, myPostFunc: help.myPost, myDate: help.myDate ,imageArray: help.myStringImageArray , postImageArray: help.myStringPostImageArray , myUId: help.myId , myPpTypeFunc: help.myPTYPE , myIId: help.myId) 



        kUserDefault.set(self.ID, forKey: "ID") 
        kUserDefault.set(self.PTYPE, forKey: "PTYPE") 
        kUserDefault.set(self.UID, forKey: "UID") 
        kUserDefault.synchronize() 


       } 




      } 
      DispatchQueue.main.async(execute: {() -> Void in 

       self.myTable.reloadData() 
      }) 
      break 

     case .failure(_): 
      print(response.result.error!) 
      break 

     } 


    } 




} 




func myFunction(nameFunc: String , yummFunc : String , myIdFunc : String , RebcolFunc: String , UTYPIMGFunc : String , Created_onFunc : String, UTYPFunc: String , commentsFunc : String , DetailsFunc : String ,myPostFunc : String , myDate : String ,imageArray : String , postImageArray : String , myUId : String , myPpTypeFunc : String , myIId : String) 
{ 



    let delegate = UIApplication.shared.delegate as? AppDelegate 

    if let context = delegate?.persistentContainer.viewContext{ 


     let person = NSEntityDescription.insertNewObject(forEntityName: "Mydata", into: context) as! Mydata 


     person.setValue(nameFunc, forKey: "name") 

     person.setValue(yummFunc, forKeyPath: "postlike") 
     person.setValue(myIdFunc, forKey: "uID") 
     person.setValue(RebcolFunc, forKey: "rebcol") 
     person.setValue(UTYPFunc, forKey: "uTYPIMG") 
     person.setValue(Created_onFunc, forKey: "created_on") 
     person.setValue(UTYPFunc, forKey: "uTYP") 
     person.setValue(commentsFunc, forKeyPath: "postcomment") 
     person.setValue(DetailsFunc, forKeyPath: "details") 
     person.setValue(myPostFunc, forKeyPath: "share") 
     person.setValue(myDate, forKeyPath: "zDate") 
     person.setValue(imageArray, forKeyPath: "profimg") 
     person.setValue(postImageArray, forKeyPath: "mid_img") 
     person.setValue(myUId, forKey: "iD") 
     person.setValue(myPpTypeFunc, forKey: "pTYPE") 


     do { 

      try(context.save()) 
     } catch let err { 
      print(err) 
     } 


     loadData() 

    } 



} 

FUNC loadData(){ 令代表= UIApplication.shared.delegate爲? AppDelegate的

if let context = delegate?.persistentContainer.viewContext { 

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Mydata") 



    do { 



     people = try(context.fetch(fetchRequest)) as? [Mydata] 





    } catch let err { 
     print(err) 
    } 

} 

}

我曾經做過這樣的,但問題是當有互聯網連接,這是工作。沒有互聯網應用chrashes