mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			66 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			66 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Swift
		
	
| 
											7 years ago
										 | // | ||
|  | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import SignalServiceKit | ||
|  | 
 | ||
|  | @objc | ||
|  | public class OWS114RemoveDynamicInteractions: OWSDatabaseMigration { | ||
|  | 
 | ||
|  |     // MARK: - Dependencies | ||
|  | 
 | ||
|  |     // MARK: - | ||
|  | 
 | ||
|  |     // Increment a similar constant for each migration. | ||
|  |     @objc | ||
|  |     class func migrationId() -> String { | ||
|  |         return "114" | ||
|  |     } | ||
|  | 
 | ||
|  |     override public func runUp(completion: @escaping OWSDatabaseMigrationCompletion) { | ||
|  |         Logger.debug("") | ||
|  |         BenchAsync(title: "\(self.logTag)") { (benchCompletion) in | ||
|  |             self.doMigrationAsync(completion: { | ||
|  |                 benchCompletion() | ||
|  |                 completion() | ||
|  |             }) | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     private func doMigrationAsync(completion : @escaping OWSDatabaseMigrationCompletion) { | ||
|  |         DispatchQueue.global().async { | ||
|  |             self.dbReadWriteConnection().readWrite { transaction in | ||
|  |                 guard let dbView = TSDatabaseView.threadSpecialMessagesDatabaseView(transaction) as? YapDatabaseViewTransaction else { | ||
|  |                     owsFailDebug("Couldn't load db view.") | ||
|  |                     return | ||
|  |                 } | ||
|  | 
 | ||
|  |                 var interactionsToDelete = [TSInteraction]() | ||
|  |                 let groupIds = dbView.allGroups() | ||
|  |                 for groupId in groupIds { | ||
|  |                     dbView.enumerateKeysAndObjects(inGroup: groupId) { (_: String, _: String, object: Any, _: UInt, _: UnsafeMutablePointer<ObjCBool>) in | ||
|  |                         guard let interaction = object as? TSInteraction else { | ||
|  |                             owsFailDebug("Invalid database entity: \(type(of: object)).") | ||
|  |                             return | ||
|  |                         } | ||
|  |                         guard interaction.isDynamicInteraction() else { | ||
|  |                             return | ||
|  |                         } | ||
|  |                         interactionsToDelete.append(interaction) | ||
|  |                     } | ||
|  |                 } | ||
|  | 
 | ||
|  |                 for interaction in interactionsToDelete { | ||
|  |                     Logger.debug("Cleaning up interaction: \(type(of: interaction)).") | ||
|  |                     interaction.remove(with: transaction) | ||
|  |                 } | ||
|  | 
 | ||
|  |                 self.save(with: transaction) | ||
|  |             } | ||
|  | 
 | ||
|  |             completion() | ||
|  |         } | ||
|  |     } | ||
|  | } |