【发布时间】:2017-11-09 19:47:37
【问题描述】:
我有以下情况,当应用程序从设备内存中抛出时,我从 Realm 中删除了非实际数据。我有一个特殊的 FriendRealmManager 类,该类包含函数 clearCache,它删除用户(目前还不是朋友)。当我在 applicationWillTerminate 函数中调用这个管理器时,当我返回应用程序后,我看到这个函数不起作用,因为有模型的用户不再是朋友。我试图将 clearCache 函数的代码移到 applicationWillTerminate 中,这很有效。请告诉我,是否可以在 applicationWillTerminate 中对不同管理器的功能执行类似操作?
普通函数和静态函数我都试过了。
没用
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
FriendRealmManager.clearCache()
}
class FriendRealmManager {
static func clearCache() {
DispatchQueue.main.async {
do {
let realm = try Realm()
try realm.write {
let nonFriendUsers = realm.objects(RealmUser.self).filter("isFriend == %@ AND isMain == %@", false, false)
realm.delete(nonFriendUsers)
}
} catch {
debugPrint(error.localizedDescription)
}
}
}
}
有效
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
do {
let realm = try! Realm()
try realm.write {
let nonFriendUsers = realm.objects(RealmUser.self).filter("isFriend == %@ AND isMain == %@", false, false)
realm.delete(nonFriendUsers)
}
} catch {
debugPrint(error.localizedDescription)
}
}
【问题讨论】:
-
尝试看看applicationWillTerminate中使用的是什么线程:
标签: ios swift realm appdelegate terminate