在 applicationWillTerminate 中删除领域数据
Removing Realm Data in the applicationWillTerminate
我有以下情况,当应用程序从设备内存中抛出时,我从 Realm 中删除了非实际数据。我有一个特殊的 FriendRealmManager class,这个 class 包含函数 clearCache,它删除用户(目前不是朋友)。当我在 applicationWillTerminate 函数中调用这个管理器时,我 return 到应用程序后,我看到这个函数不起作用,因为有不再是朋友的用户模型。我试图将 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
的官方文档,它指出
Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
因此,我建议从您的函数中删除 DispatchQueue.main.async
部分,然后同步删除 运行。
我有以下情况,当应用程序从设备内存中抛出时,我从 Realm 中删除了非实际数据。我有一个特殊的 FriendRealmManager class,这个 class 包含函数 clearCache,它删除用户(目前不是朋友)。当我在 applicationWillTerminate 函数中调用这个管理器时,我 return 到应用程序后,我看到这个函数不起作用,因为有不再是朋友的用户模型。我试图将 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
的官方文档,它指出
Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.
因此,我建议从您的函数中删除 DispatchQueue.main.async
部分,然后同步删除 运行。