ios - iCloud Sync check for duplicates Swift -
i'm trying set icloud sync existing coredata app. basic syncing working fine. i'm trying check duplicates when updating store.
i've used the official documentation this thread , tried translate code swift. here i've got far:
func checkduplicates() { let uniquepropertykey = "titel" let entityname = "entity" let keypathexpr = nsexpression(forkeypath: uniquepropertykey) let countexpr: nsexpression = nsexpression(forfunction: "count:", arguments: [keypathexpr]) let countexprdesc = nsexpressiondescription() countexprdesc.name = "count" countexprdesc.expression = countexpr countexprdesc.expressionresulttype = nsattributetype.integer64attributetype let entity: nsentitydescription = nsentitydescription.entityforname(entityname, inmanagedobjectcontext: moc)! let uniqueattr: nsattributedescription = (entity.attributesbyname[uniquepropertykey]! as? nsattributedescription)! let sortdescriptor = nssortdescriptor(key: "timestamp", ascending: true) let fr = nsfetchrequest(entityname: entityname) fr.resulttype = nsfetchrequestresulttype.dictionaryresulttype fr.propertiestofetch = [uniqueattr, countexprdesc] fr.propertiestogroupby = [uniqueattr] fr.sortdescriptors = [sortdescriptor] let fetcheddictionaries: nsarray = self.fetchdictionarieswithrequest(fr) println(fetcheddictionaries) var valueswithdupes: nsmutablearray? dict in fetcheddictionaries { let count: int = dict["count"] as! int if count > 1 { valueswithdupes!.addobject(dict[uniquepropertykey]!!) } } println(valueswithdupes) let dupefr = nsfetchrequest(entityname: entityname) dupefr.includespendingchanges = false dupefr.fetchbatchsize = 20 let predicate = nspredicate(format: "\(uniquepropertykey) in %@", valueswithdupes!) dupefr.predicate = predicate let dupes: nsarray = self.fetchdictionarieswithrequest(dupefr) println(dupes) } func fetchdictionarieswithrequest (request: nsfetchrequest) -> nsarray { var fetchedresultscontroller = nsfetchedresultscontroller(fetchrequest: request, managedobjectcontext: moc, sectionnamekeypath: nil, cachename: nil) println(fetchedresultscontroller.fetchedobjects) var error: nserror? = nil if fetchedresultscontroller.performfetch(&error) { println("error when fetching records: \(error)") return nil } return fetchedresultscontroller.fetchedobjects! }
first , obvious problem error check in fetchdictionarieswithrequest
cannot return nil here
second fetchedresultscontroller
returns nil. how fetchrequest looks in logs:
<nsfetchrequest: 0x1744c4280> (entity: entity; predicate: ((null)); sortdescriptors: (( "(timestamp, ascending, compare:)" )); type: nsdictionaryresulttype; includespendingchanges: no; propertiestofetch: (( "(<nsattributedescription: 0x17012b040>), name titel, isoptional 1, istransient 0, entity entity, renamingidentifier titel, validation predicates (\n), warnings (\n), versionhashmodifier (null)\n userinfo {\n}, attributetype 700 , attributevalueclassname nsstring, defaultvalue (null)", "(<nsexpressiondescription: 0x170171f40>), name count, isoptional 1, istransient 0, entity (null), renamingidentifier count, validation predicates (\n), warnings (\n), versionhashmodifier (null)\n userinfo {\n}" )); propertiestogroupby: (( "(<nsattributedescription: 0x17012b040>), name titel, isoptional 1, istransient 0, entity entity, renamingidentifier titel, validation predicates (\n), warnings (\n), versionhashmodifier (null)\n userinfo {\n}, attributetype 700 , attributevalueclassname nsstring, defaultvalue (null)" )); )
however i'm stuck. have typos in code? did translate wrong objective-c? know of working example of in swift somewhere in net?
any appreciated!
thx seb
Comments
Post a Comment