diff --git a/Yep/AppDelegate.swift b/Yep/AppDelegate.swift index 53cc5816..4f9a4ebf 100644 --- a/Yep/AppDelegate.swift +++ b/Yep/AppDelegate.swift @@ -43,7 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier(YepConfig.appGroupID)! let realmPath = directory.URLByAppendingPathComponent("db.realm").path! - return Realm.Configuration(path: realmPath, schemaVersion: 22, migrationBlock: { migration, oldSchemaVersion in + return Realm.Configuration(path: realmPath, schemaVersion: 23, migrationBlock: { migration, oldSchemaVersion in }) } diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 4214190a..61c612fa 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -592,8 +592,13 @@ class Message: Object { realm.delete(mediaMetaData) } + // 除非没有谁指向 openGraphInfo,不然不能删除它 if let openGraphInfo = openGraphInfo { - realm.delete(openGraphInfo) + if openGraphInfo.feeds.isEmpty { + if openGraphInfo.messages.count == 1, let first = openGraphInfo.messages.first where first == self { + realm.delete(openGraphInfo) + } + } } switch mediaType { @@ -829,6 +834,13 @@ class OpenGraphInfo: Object { dynamic var infoDescription: String = "" dynamic var thumbnailImageURLString: String = "" + var messages: [Message] { + return linkingObjects(Message.self, forProperty: "openGraphInfo") + } + var feeds: [Feed] { + return linkingObjects(Feed.self, forProperty: "openGraphInfo") + } + override class func primaryKey() -> String? { return "URLString" } @@ -926,6 +938,15 @@ class Feed: Object { realm.delete(location) } + // 除非没有谁指向 openGraphInfo,不然不能删除它 + if let openGraphInfo = openGraphInfo { + if openGraphInfo.messages.isEmpty { + if openGraphInfo.feeds.count == 1, let first = openGraphInfo.messages.first where first == self { + realm.delete(openGraphInfo) + } + } + } + realm.delete(self) } }