处理 UICollectionView 提要监听器的最佳实践
Best Practise to handle listeners for a UICollectionView feed
我正在尝试为以下情况找到正确的方法:
我有 UICollectionView
显示对象(具有唯一 ID)。每个用户都可以 "modify" 对象,因此显然所有其他用户同时需要看到此更改。
问题是,我应该听这个对象的父对象 Value
吗,它包含所有对象(这听起来像是浪费 "request time",因为有时用户将监听他们不需要的更改(以防 collectionView 被过滤),或者我应该设置一堆(单个)监听器,只监听当前需要的 "specific" 个对象?
谢谢!
您可以使用子事件来处理对单个子节点的更新。
// save updates a dictionary with the snapshot key
// being the dictionary key
var items = [String: FDataSnapshot]()
let ref = Firebase(url: "<my-firebase-app>")
// listen to various child events and update the data source
ref.observeEventType(.ChildAdded) { (snap: FDataSnapshot!) in
items[snap.key] = snap
}
ref.observeEventType(.ChildChanged) { (snap: FDataSnapshot!) in
items[snap.key] = snap
}
ref.observeEventType(.ChildRemoved) { (snap: FDataSnapshot!) in
items.removeValueForKey(snap.key)
}
...should i listen to the parent Value of this objects,which contain
all of the objects(which sounds like waste of "request time"
重要的是不要考虑请求。 Firebase 使用 WebSocket。一旦建立了 WebSocket,它就会与服务器保持持久连接。在套接字之间来回发送数据几乎不需要开销。
But, I would actually use FirebaseUI,这是一个用于将 Firebase 数据同步到 UITableView 和 UICollectionView 的库。
let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let dataSource: FirebaseCollectionViewDataSource!
...
self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, cellReuseIdentifier: "<YOUR-REUSE-IDENTIFIER>", view: self.collectionView)
self.dataSource.populateCellWithBlock { (cell: UICollectionViewCell, obj: NSObject) -> Void in
let snap = obj as! FDataSnapshot
// Populate cell as you see fit, like as below
cell.backgroundColor = UIColor.blueColor()
}
self.collectionView.dataSource = self.dataSource
我正在尝试为以下情况找到正确的方法:
我有 UICollectionView
显示对象(具有唯一 ID)。每个用户都可以 "modify" 对象,因此显然所有其他用户同时需要看到此更改。
问题是,我应该听这个对象的父对象 Value
吗,它包含所有对象(这听起来像是浪费 "request time",因为有时用户将监听他们不需要的更改(以防 collectionView 被过滤),或者我应该设置一堆(单个)监听器,只监听当前需要的 "specific" 个对象?
谢谢!
您可以使用子事件来处理对单个子节点的更新。
// save updates a dictionary with the snapshot key
// being the dictionary key
var items = [String: FDataSnapshot]()
let ref = Firebase(url: "<my-firebase-app>")
// listen to various child events and update the data source
ref.observeEventType(.ChildAdded) { (snap: FDataSnapshot!) in
items[snap.key] = snap
}
ref.observeEventType(.ChildChanged) { (snap: FDataSnapshot!) in
items[snap.key] = snap
}
ref.observeEventType(.ChildRemoved) { (snap: FDataSnapshot!) in
items.removeValueForKey(snap.key)
}
...should i listen to the parent Value of this objects,which contain all of the objects(which sounds like waste of "request time"
重要的是不要考虑请求。 Firebase 使用 WebSocket。一旦建立了 WebSocket,它就会与服务器保持持久连接。在套接字之间来回发送数据几乎不需要开销。
But, I would actually use FirebaseUI,这是一个用于将 Firebase 数据同步到 UITableView 和 UICollectionView 的库。
let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let dataSource: FirebaseCollectionViewDataSource!
...
self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, cellReuseIdentifier: "<YOUR-REUSE-IDENTIFIER>", view: self.collectionView)
self.dataSource.populateCellWithBlock { (cell: UICollectionViewCell, obj: NSObject) -> Void in
let snap = obj as! FDataSnapshot
// Populate cell as you see fit, like as below
cell.backgroundColor = UIColor.blueColor()
}
self.collectionView.dataSource = self.dataSource