ios - Issue Saving/Displaying CKAssets -
i'm pretty new swift, please try bear me.
i'm able download ckrecords , insert them array, "birdfacts". each record includes few strings, image (ckasset), date, , int. when download icloud, works fine.
everything saving expected, except image. when reload data, asset doesn't show up.
here code load saved data:
if let savedfacts = loadfacts() { birdfacts = savedfacts print("successfully loaded saved bird facts") } func loadfacts() -> [ckrecord]? { return nskeyedunarchiver.unarchiveobjectwithfile(birdfact.archiveurl.path!) as? [ckrecord] }
this code save array:
func savefacts() { let issuccessfulsave = nskeyedarchiver.archiverootobject(birdfacts, tofile: birdfact.archiveurl.path!) if !issuccessfulsave { print("failed save bird facts") } }
this within custom class definition file:
import uikit import cloudkit class birdfact: nsobject, nscoding { //mark: properties var birdname: string var photo: ckasset var birdfact: string var date: nsdate var sortingdate: int //mark: types struct propertykey { static let namekey = "name" static let photokey = "photo" static let factkey = "fact" static let datekey = "date" static let sortingdatekey = "sortingdate" } //mark: initialization init?(birdname: string, photo: ckasset, birdfact: string, date: nsdate, sortingdate: int){ //init stored props self.birdname = birdname self.photo = photo self.birdfact = birdfact self.date = date self.sortingdate = sortingdate super.init() if birdname.isempty || birdfact.isempty { return nil } } //mark: nscoding func encodewithcoder(acoder: nscoder) { acoder.encodeobject(birdname, forkey: propertykey.namekey) acoder.encodeobject(photo, forkey: propertykey.photokey) acoder.encodeobject(birdfact, forkey: propertykey.factkey) acoder.encodeobject(date, forkey: propertykey.datekey) acoder.encodeobject(sortingdate, forkey: propertykey.sortingdatekey) } required convenience init?(coder adecoder: nscoder) { let birdname = adecoder.decodeobjectforkey(propertykey.namekey) as! string let photo = adecoder.decodeobjectforkey(propertykey.photokey) as! ckasset let birdfact = adecoder.decodeobjectforkey(propertykey.factkey) as! string let date = adecoder.decodeobjectforkey(propertykey.datekey) as! nsdate let sortingdate = adecoder.decodeobjectforkey(propertykey.sortingdatekey) as! int self.init(birdname: birdname, photo: photo, birdfact: birdfact, date: date, sortingdate: sortingdate) } //mark: archiving paths static let documentsdirectory = nsfilemanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask).first! static let archiveurl = documentsdirectory.urlbyappendingpathcomponent("birdfacts")
}
i'm saving whenever finish downloading , sorting records. idea going wrong?
edit: included cloudkit query. should note never save icloud, download existing records.
func allprevfacts(date: string, olddate: int){ act.startanimating() let container = ckcontainer.defaultcontainer() let publicdb = container.publicclouddatabase //let factpredicate = nspredicate(format: "recordid = %@", ckrecordid(recordname: date)) let factpredicate = nspredicate(format: "date <= %@", nsdate()) let query = ckquery(recordtype: "birdfacts", predicate: factpredicate) publicdb.performquery(query, inzonewithid: nil) { (results, error) -> void in if error != nil { print(error) } else { dispatch_async(dispatch_get_main_queue()){ //print(results) var count = 0 var sortedresults = [int]() result in results! { var b = result.valueforkey("sortingdate") as! int sortedresults.append(b) } print(sortedresults) while count < sortedresults.count { if sortedresults[count] <= olddate { sortedresults.removeatindex(count) } else { count = count + 1 } } print(sortedresults) while sortedresults.count > 0 { var d: int = 0 let = sortedresults.maxelement() print(a) while d < sortedresults.count{ if sortedresults[d] == { sortedresults.removeatindex(d) self.birdfacts.append(results![d]) self.tablefacts.reloaddata() self.tablefacts.hidden = false } d = d + 1 print(d) } } self.savefacts() print("saving bird facts") self.tablefacts.hidden = false } } } act.stopanimating()