我有一个didPressSend函数,它接受在textview中编写的文本,并将其作为String插入到上下文中。一旦插入上下文,NSFetchedResultsController的DidChangeContent会检测到某些内容被插入到上下文中并相应地自动更新集合View(请记住,DidChangeContent绝对是空的)。这是我刚刚为该部分编写的代码。 (使用text创建消息是插入上下文的函数)。
func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String! ,senderDisplayName: String!, date: NSDate){
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
createMessageWithText(text, friend: friend!, context: moc, date: date)
do {
try context.save()
} catch let err {
print(err)
}
}
private func createMessageWithText(text: String, friend: Friend, context: NSManagedObjectContext, date: NSDate, isSender: Bool = false) -> Mesages {
let message = NSEntityDescription.insertNewObjectForEntityForName("Mesages", inManagedObjectContext: context) as! Mesages
message.user = friend
message.text = text
message.timestamp = date
message.isSender = isSender
friend.lastMessage = message
return message
}
}
一旦插入上下文,didChangeContent就会检测到它并自动更新集合视图:
lazy var fetchedResultsControler: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest(entityName: "Mesages")
fetchRequest.fetchBatchSize = 50
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "user.id = %@", self.friend!.id!)
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
return frc
}()
func controllerDidChangeContent(controller: NSFetchedResultsController) {
}
奇怪的是,在DidChangeContent中绝对没有任何东西似乎负责填充collectionView,请记住我用fetchResultsController中的对象数量填充collectionView:
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) ->
Int {
if let count = fetchedResultsControler.sections?[0].numberOfObjects {
return count
}
return 0
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
return cell
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let msg : Mesages = fetchedResultsControler.objectAtIndexPath(indexPath) as! Mesages
let messageData = JSQMessage(senderId: "" , displayName: "Me", text: msg.text)
return messageData
}
我还有一个来自firebase的监听器,无论何时向我发送消息,它都会立即检测到它,插入上下文,保存,nsfetchResultsController检测到它,并触发didChangeContent。奇怪的是,它不会在这种情况下自动更新,除非我把collectionView.reloadData放在didChangeContent中,但我不需要为didPressSend放置collectionView.reloadData,因为它自己神奇地自动更新。以与didPressSend相同的方式插入上下文的firebase侦听器的代码如下:(使用text创建消息是插入上下文的函数)。
ref2.observeEventType(.ChildAdded, withBlock: {(snapshot2) in
let messageId = snapshot2.key
//first message ID out of 20.
let messagesRef = FIRDatabase.database().reference().child("messages").child(messageId)
messagesRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
let dictionary = snapshot.value as! [NSObject: AnyObject]
let timestamp = dictionary["timestamp"] as! NSNumber
let doubleTimestamp = Double(timestamp)
let date = NSDate(timeIntervalSinceReferenceDate: (doubleTimestamp))
self.createMessageWithText(dictionary["text"]! as! String, friend: friend, context: context, date: date, isSender: true)
do {
try context.save()
}
catch {
print(error)
}
private func createMessageWithText(text: String, friend: Friend, context: NSManagedObjectContext, date: NSDate, isSender: Bool = false) -> Mesages {
let message = NSEntityDescription.insertNewObjectForEntityForName("Mesages", inManagedObjectContext: context) as! Mesages
message.user = friend
message.text = text
message.timestamp = date
message.isSender = isSender
friend.lastMessage = message
return message
}
那就是说,我不确定为什么以及如何使用NSfetchedResultsController填充collectionView。 (我真的担心DidPressSend正在填充集合视图,而不会重新加载任何类型的数据)。另外还有一个bug,如果我删除didChangeContent,你可以看到包含NOTHING,didPressSend会停止自动更新。当我添加空的didChangeContent时,集合View开始自动更新(仅适用于didpressSend)。
答案 0 :(得分:0)
在委托方法下面实施,并且collectionView或TableView将更新同步。尽快获取新数据。
from functools import reduce
from pyspark.sql.functions import udf
from pyspark.sql.types import IntegerType
# UDF definition
find_diff = udf(lambda a: reduce(lambda x, y: x - y, a), IntegerType())
(
df.
withColumn(
'diff',
find_diff('list')
).
show(truncate=False)
)
+-------+------+----+
|version|list |diff|
+-------+------+----+
|v1 |[5, 2]|3 |
|v1 |[2, 5]|-3 |
|v1 |[3, 2]|1 |
|v2 |[2] |2 |
|v2 |[1, 2]|-1 |
|v2 |[1] |1 |
+-------+------+----+