swift2 - Reorder UicollectioView items using RealmSwift on drag and drop -
i'm trying reorder uicollectionviewcell images on drag , drop using realmswift database, ui not updating on drag , drop , strange behaviour, images duplicating , code this
realmmodel as
class storyalbumdm: object { dynamic var id = 0 dynamic var type = "" dynamic var isimage: int = 0 dynamic var textdata = "" dynamic var imagedata: nsdata? = nil dynamic var rowid: int = 0 dynamic var position: int = 0 dynamic var storyid: int = 0 dynamic var iscoverimage: int = 0 dynamic var imagepath = "" let allstories = list<storyalbumdm>() }
on drag , drop i'm doing this
func collectionview(collectionview: uicollectionview, atindexpath: nsindexpath, didmovetoindexpath toindexpath: nsindexpath) { print("moveitematindexpath") let fromindexpath: int = atindexpath.row print("from", fromindexpath) let toindexpathint: int = toindexpath.row print("to", toindexpath) let fromdata: storyalbumdm! fromdata = realm.objects(storyalbumdm.self).filter("position = %d , storyid = %d", fromindexpath, self.storyid).first! let todata: storyalbumdm! todata = realm.objects(storyalbumdm.self).filter("position = %d , storyid = %d", toindexpath, self.storyid).first! var tempdata = storyalbumdm() self.performselectoronmainthread(#selector(storyviewcontroller.updatesrtoryinrealm), withobject: self.collectionview, waituntildone: true) dispatch_async(dispatch_get_main_queue(), { self.collectionview.performbatchupdates({ self.collectionview.reloaddata() }, completion: nil) }) } func updatesrtoryinrealm() { self.tempdata.type = self.todata.type self.tempdata.isimage = self.todata.isimage self.tempdata.textdata = self.todata.textdata self.tempdata.rowid = self.todata.rowid self.tempdata.imagedata = self.todata.imagedata self.tempdata.position = self.todata.position self.tempdata.storyid = self.todata.storyid self.tempdata.iscoverimage = self.todata.iscoverimage self.tempdata.imagepath = self.todata.imagepath { try! realm.write { self.todata.type = self.fromdata.type self.todata.isimage = self.fromdata.isimage self.todata.textdata = self.fromdata.textdata self.todata.rowid = self.fromdata.rowid self.todata.imagedata = self.fromdata.imagedata self.todata.position = self.fromdata.position self.todata.storyid = self.fromdata.storyid self.todata.iscoverimage = self.fromdata.iscoverimage self.todata.imagepath = self.fromdata.imagepath // title.id = temp.id self.fromdata.type = self.tempdata.type self.fromdata.isimage = self.tempdata.isimage self.fromdata.textdata = self.tempdata.textdata self.fromdata.rowid = self.tempdata.rowid self.fromdata.imagedata = self.tempdata.imagedata self.fromdata.position = self.tempdata.position self.fromdata.storyid = self.tempdata.storyid self.fromdata.iscoverimage = self.tempdata.iscoverimage self.fromdata.imagepath = self.tempdata.imagepath } //} } catch { print("printed error : ") }
problem: images duplicating, not updating on ui , reorder strange behaviour, please me on this
i answered a similar question recently, i'll re-explain here. :)
easily, best , quickest way re-order realm objects inside realm file make overarching list
object holds of realm objects of given type.
for example in case, make object hold allstories
value created:
// model class manages ordering of story album objects class storyalbumdmlist: object { let allstories = list<storyalbumdm>() } // model class actual story album objects class storyalbumdm: object { dynamic var id = 0 dynamic var type = "" dynamic var isimage: int = 0 dynamic var textdata = "" dynamic var imagedata: nsdata? = nil dynamic var rowid: int = 0 dynamic var position: int = 0 dynamic var storyid: int = 0 dynamic var iscoverimage: int = 0 dynamic var imagepath = "" }
this way, when want re-order list, need re-order them inside array.
like said in other question, 1 other way can (which not good, doesn't require realm object) add property named orderedindex
, contains number indicating numerical order of these objects. when want re-order them, it's matter of re-setting these numbers.
let me know if need more clarification!
Comments
Post a Comment