注意:请参阅底部“编辑”以获得答案。
我正在编写一个自定义NSObject
,它将自动异步初始化,然后在完成后发送NSNotification
,以便我可以将其附加到UITableView
的数据源。这是班级:
class Friend: NSObject {
var userID: String!
var schedule: Schedule!
init(friendUserID: String) {
super.init()
print("Initializing Friend.")
self.userID = friendUserID
let notificationIdentifier = friendUserID + "hasFinishedDownloading"
print("Adding observer for:", notificationIdentifier)
nc.addObserver(self, selector: #selector(haveFinishedDownload), name: Notification.Name(notificationIdentifier), object: nil)
let friendSchedule = Schedule(userID: friendUserID)
self.schedule = friendSchedule
}
@objc func haveFinishedDownload(notification: Notification) {
print("Download Finished.")
print(notification)
}
}
在添加观察者时,我为“selector”属性尝试了几种不同的东西,运气很少。
以下是发送通知时的代码:
class Schedule: NSObject {
//The Key is what day it meets period 4
//The value is an SSAClass object
var ssaClassesDictionary:Dictionary<Int, SSAClass> = [:]
func addClassToDicitonary(classObject: SSAClass!) {
self.ssaClassesDictionary[classObject.day!] = classObject
}
func fillClasses(userID: String!) {
print("Filling Classses")
var userOrTeacher: String!
switch isUserTeacher(user: userID) {
case true:
userOrTeacher = "Teachers"
case false:
userOrTeacher = "Users"
}
print("Ref about to run")
ref.child(userOrTeacher).child(userID!).child("c").observe(.value, with: {(snapshot) in
print("Ref has started downloading")
if (snapshot.value != nil) {
let snapshotValue: [String] = snapshot.value as! [String]
print("snapshot.value != nil")
var numberDownloaded = 0
for i in snapshotValue {
print("running for loop iteration:", i)
ref.child("Classes").child(i).observeSingleEvent(of: .value, with: {(snapshot) in
let value = snapshot.value as! Dictionary<String, Any>
//"year", "1", "2", or "3"
var customTerm: String!
//"year", 1, 2, or 3
let term = value["term"]
switch term {
case is String.Type:
customTerm = "year"
case is Int.Type:
customTerm = "\(term!)"
default: customTerm = "year"
}
let ssaClass = SSAClass(classID: i, AB: value["AB"] as! Bool, Sci: value["Sci"] as! Bool, name: value["n"] as! String, room: value["room"] as! String, term: customTerm, day: value["d"] as! Int)
if ssaClass.term == "year" || ssaClass.term == "3" {
self.addClassToDicitonary(classObject: ssaClass)
}
numberDownloaded = numberDownloaded + 1
})
}
DispatchQueue.global(qos: .background).async {
//This is run on the background queue
print("Running asynchronous Code")
while numberDownloaded < snapshotValue.count {
}
DispatchQueue.main.async {
print("Finished While Loop, going back to main thread")
//This is run on the main queue, after the previous code in outer block
//Post notification
let notificationIdentifier = userID + "hasFinishedDownloading"
print("Posting Notification for:", notificationIdentifier)
nc.post(name: Notification.Name(notificationIdentifier), object: nil)
}
}
}
})
}
init(userID: String!) {
super.init()
fillClasses(userID: userID)
}
}
所有打印输出都会运行:
Initializing Friend.
Adding observer for: 19wangdhasFinishedDownloading
Filling Classses
Ref about to run
Default
Ref has started downloading
snapshot.value != nil
running for loop iteration: CH402 - 1
running for loop iteration: CH404 - 1
running for loop iteration: CH406 - 2
running for loop iteration: CS400 - 11
running for loop iteration: CS411 - 11
running for loop iteration: EN301 - 3
running for loop iteration: EN302 - 1
running for loop iteration: EN4014 - 2
running for loop iteration: FR360 - 1
running for loop iteration: HI300 - 8
running for loop iteration: MA405 - 2
running for loop iteration: MA406 - 1
running for loop iteration: MA501 - 2
running for loop iteration: MA506 - 1
Running asynchronous Code
Finished While Loop, going back to main thread
Posting Notification for: 19wangdhasFinishedDownloading
除了“下载完成”。
非常感谢所有帮助。
编辑:接受的答案指出了问题所在。我只是测试我的代码而没有实际在任何地方实现初始化的类。结果,ARC完成了我的Friend对象以保存内存。出于测试目的,我通过将其附加到全局数组并在每次按下我的应用程序中的随机按钮时打印数组的内容来修复它,使ARC认为我在某种程度上使用它。
答案 0 :(得分:1)
您的Friend
实例可能已被ARC自动解除分配。你有强烈的参考吗?
将这些行添加到Friend
课程中以查看它:
deinit {
print("\(userID) deallocated")
}