ios - NSUserDefaults not saving to disk with sharing extension -


i trying save data nsuserdefaults later use share extension. however, data never saved when re-open app. likewise extension not able retrieve data

i have singleton class called appuser has username property. if set

appuser.username = "bob"

and run saveappdata()

class func saveappdata() {      let data = nskeyedarchiver.archiveddatawithrootobject(appuser.singleton)     var shareddefaults =  nsuserdefaults(suitename: "group.ca.mycompany.myapp")     shareddefaults!.setobject(data, forkey: "appuser")     shareddefaults!.synchronize() } 

close app & re-open

class func loadappdata() {      var shareddefaults = nsuserdefaults(suitename: "group.ca.mycompany.myapp")      if let data = shareddefaults!.objectforkey("appuser") as? nsdata {         println("unarchived file succesfully")         nskeyedunarchiver.unarchiveobjectwithdata(data) //should throw data appuser.singleton class         println("username is: \(appuser.singleton.username)")     } } 

when run loadappdata() after app has been reopened following lines in console:

unarchived file succesfully username nil 

why username being returned nil though ran setobject , sychronize?

note : have "suitname" set correctly in app "group.ca.mycompany.myapp". have appgroups enabled both app , extension "group.ca.mycompany.myapp".

so, doesn't saving unarchived "data" contents object, here's example me saving unacrhived data contents array of strings:

let data = nskeyedarchiver.archiveddatawithrootobject(["asdf", "asdf"]) var shareddefaults =  nsuserdefaults(suitename: "group.ca.mycompany.myapp") shareddefaults!.setobject(data, forkey: "appuser") shareddefaults!.synchronize()  println(shareddefaults!.valueforkey("appuser"))  var shareddefaults1 =  nsuserdefaults(suitename: "group.ca.mycompany.myapp")  if let data = shareddefaults1!.objectforkey("appuser") as? nsdata {     println("unarchived file succesfully")     let = nskeyedunarchiver.unarchiveobjectwithdata(data) as? [string] //should throw data appuser.singleton class     println("username is: \(something)") } 

and gives following information:

optional(<62706c69 73743030 d4010203 0405081a 1b542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748001 a5090a11 12135524 6e756c6c d20b0c0d 0e562463 6c617373 5a4e532e 6f626a65 63747380 04a20f10 80028003 54617364 66546173 6466d214 15161958 24636c61 73736573 5a24636c 6173736e 616d65a2 1718574e 53417272 6179584e 534f626a 65637457 4e534172 72617912 000186a0 5f100f4e 534b6579 65644172 63686976 65720811 161f2832 353a3c42 484d545f 61646668 6d727780 8b8e969f a7ac0000 00000000 01010000 00000000 001c0000 00000000 00000000 00000000 00be>)

unarchived file succesfully username is: optional(["asdf", "asdf"])

the difference here i'm saving unarchived contents "something":

let = nskeyedunarchiver.unarchiveobjectwithdata(data) as? [string] 

and such, "something" proxy singleton class.

to make useful you, here's implmentation:

let data = nskeyedarchiver.archiveddatawithrootobject(["asdf", "asdf"]) var shareddefaults =  nsuserdefaults(suitename: "group.ca.mycompany.myapp") shareddefaults!.setobject(data, forkey: "appuser") shareddefaults!.synchronize()  var shareddefaults1 =  nsuserdefaults(suitename: "group.ca.mycompany.myapp")  if let data = shareddefaults1!.objectforkey("appuser") as? nsdata {     println("unarchived file succesfully")     let = nskeyedunarchiver.unarchiveobjectwithdata(data) as! [string] //should throw data appuser.singleton class     println(something[0]) } 

this new implementation prints out this:

asdf 

this means can save unarchived data object , store object appuser.singleton ease, long set appuser model class correctly.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -