clear noGroupFeeds & notJoinedFeeds

This commit is contained in:
nixzhu
2015-12-07 11:28:30 +08:00
parent 42ac632252
commit d43f693bb3
2 changed files with 59 additions and 4 deletions
+4
View File
@@ -117,6 +117,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
println("Enter background")
NSNotificationCenter.defaultCenter().postNotificationName(MessageToolbar.Notification.updateDraft, object: nil)
clearUselessRealmObjects()
}
func applicationWillEnterForeground(application: UIApplication) {
@@ -151,6 +153,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
clearUselessRealmObjects()
}
// MARK: APNs
+55 -4
View File
@@ -709,6 +709,15 @@ class FeedAudio: Object {
return nil
}
func deleteAudioFile() {
guard !fileName.isEmpty else {
return
}
NSFileManager.removeMessageAudioFileWithName(fileName)
}
}
class FeedLocation: Object {
@@ -767,6 +776,22 @@ class Feed: Object {
realm.delete(socialWork)
}
if let audio = audio {
audio.deleteAudioFile()
realm.delete(audio)
}
if let location = location {
if let coordinate = location.coordinate {
realm.delete(coordinate)
}
realm.delete(location)
}
realm.delete(self)
}
}
@@ -1507,16 +1532,15 @@ func tryDeleteOrClearHistoryOfConversation(conversation: Conversation, inViewCon
vc.presentViewController(deleteAlertController, animated: true, completion: nil)
}
func clearOldObjects() {
func clearUselessRealmObjects() {
guard let realm = try? Realm() else {
return
}
realm.beginWrite()
println("do clearUselessRealmObjects")
// 7
let oldThresholdUnixTime = NSDate(timeIntervalSinceNow: -(60 * 60 * 24 * 7)).timeIntervalSince1970
realm.beginWrite()
// User
@@ -1525,9 +1549,14 @@ func clearOldObjects() {
// Message
do {
// 7
let oldThresholdUnixTime = NSDate(timeIntervalSinceNow: -(60 * 60 * 24 * 1)).timeIntervalSince1970
let predicate = NSPredicate(format: "createdUnixTime < %f", oldThresholdUnixTime)
let oldMessages = realm.objects(Message).filter(predicate)
println("oldMessages.count: \(oldMessages.count)")
oldMessages.forEach({
$0.deleteAttachmentInRealm(realm)
realm.delete($0)
@@ -1536,6 +1565,28 @@ func clearOldObjects() {
// Feed
do {
let predicate = NSPredicate(format: "group == nil")
let noGroupFeeds = realm.objects(Feed).filter(predicate)
println("noGroupFeeds.count: \(noGroupFeeds.count)")
noGroupFeeds.forEach({
$0.cascadeDeleteInRealm(realm)
})
}
do {
let predicate = NSPredicate(format: "group != nil AND group.includeMe = false")
let notJoinedFeeds = realm.objects(Feed).filter(predicate)
println("notJoinedFeeds.count: \(notJoinedFeeds.count)")
notJoinedFeeds.forEach({
$0.cascadeDeleteInRealm(realm)
})
}
let _ = try? realm.commitWrite()
}