@@ -303,6 +303,76 @@ public extension ParseInstallation {
303
303
Self . updateInternalFieldsCorrectly ( )
304
304
}
305
305
}
306
+
307
+ /**
308
+ Copy the `ParseInstallation` *asynchronously* based on the `objectId`.
309
+ On success, this saves the `ParseInstallation` to the keychain, so you can retrieve
310
+ the current installation using *current*.
311
+
312
+ - parameter objectId: The **id** of the `ParseInstallation` to become.
313
+ - parameter copyEntireInstallation: When **true**, copies the entire `ParseInstallation`.
314
+ When **false**, only the `channels` and `deviceToken` are copied; resulting in a new
315
+ `ParseInstallation` for original `sessionToken`. Defaults to **true**.
316
+ - parameter options: A set of header options sent to the server. Defaults to an empty set.
317
+ - parameter callbackQueue: The queue to return to after completion. Default value of .main.
318
+ - parameter completion: The block to execute.
319
+ It should have the following argument signature: `(Result<Self, ParseError>)`.
320
+ - note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
321
+ desires a different policy, it should be inserted in `options`.
322
+ */
323
+ static func become( _ objectId: String ,
324
+ copyEntireInstallation: Bool = true ,
325
+ options: API . Options = [ ] ,
326
+ callbackQueue: DispatchQueue = . main,
327
+ completion: @escaping ( Result < Self , ParseError > ) -> Void ) {
328
+ guard var currentInstallation = Self . current else {
329
+ let error = ParseError ( code: . unknownError,
330
+ message: " Current installation does not exist " )
331
+ callbackQueue. async {
332
+ completion ( . failure( error) )
333
+ }
334
+ return
335
+ }
336
+ guard currentInstallation. objectId != objectId else {
337
+ // If the installationId's are the same, assume successful replacement already occured.
338
+ callbackQueue. async {
339
+ completion ( . success( currentInstallation) )
340
+ }
341
+ return
342
+ }
343
+ currentInstallation. objectId = objectId
344
+ currentInstallation. fetch ( options: options, callbackQueue: callbackQueue) { result in
345
+ switch result {
346
+ case . success( var updatedInstallation) :
347
+ if copyEntireInstallation {
348
+ updatedInstallation. updateAutomaticInfo ( )
349
+ Self . currentContainer. installationId = updatedInstallation. installationId
350
+ Self . currentContainer. currentInstallation = updatedInstallation
351
+ } else {
352
+ Self . current? . channels = updatedInstallation. channels
353
+ if Self . current? . deviceToken == nil {
354
+ Self . current? . deviceToken = updatedInstallation. deviceToken
355
+ }
356
+ }
357
+ Self . saveCurrentContainerToKeychain ( )
358
+ guard let latestInstallation = Self . current else {
359
+ let error = ParseError ( code: . unknownError,
360
+ message: " Had trouble migrating the installation " )
361
+ callbackQueue. async {
362
+ completion ( . failure( error) )
363
+ }
364
+ return
365
+ }
366
+ latestInstallation. save ( options: options,
367
+ callbackQueue: callbackQueue,
368
+ completion: completion)
369
+ case . failure( let error) :
370
+ callbackQueue. async {
371
+ completion ( . failure( error) )
372
+ }
373
+ }
374
+ }
375
+ }
306
376
}
307
377
308
378
// MARK: Automatic Info
@@ -1533,68 +1603,27 @@ public extension ParseInstallation {
1533
1603
- warning: When initializing the Swift SDK, `migratingFromObjcSDK` should be set to **false**
1534
1604
when calling this method.
1535
1605
- warning: The latest **PFInstallation** from the Objective-C SDK should be saved to your
1536
- Parse Server before calling this method.
1606
+ Parse Server before calling this method. This method assumes **PFInstallation.installationId**
1607
+ is saved to the Keychain. If the **installationId** is not saved to the Keychain, this method will
1608
+ not work.
1537
1609
*/
1610
+ @available ( * , deprecated, message: " This does not work, use become() instead " )
1538
1611
static func migrateFromObjCKeychain( copyEntireInstallation: Bool = true ,
1539
1612
options: API . Options = [ ] ,
1540
1613
callbackQueue: DispatchQueue = . main,
1541
1614
completion: @escaping ( Result < Self , ParseError > ) -> Void ) {
1542
1615
guard let objcParseKeychain = KeychainStore . objectiveC,
1543
- let oldInstallationId: String = objcParseKeychain. object ( forKey: " installationId " ) else {
1616
+ let oldInstallationId: String = objcParseKeychain. objectObjectiveC ( forKey: " installationId " ) else {
1544
1617
let error = ParseError ( code: . unknownError,
1545
1618
message: " Could not find Installation in the Objective-C SDK Keychain " )
1546
1619
callbackQueue. async {
1547
1620
completion ( . failure( error) )
1548
1621
}
1549
1622
return
1550
1623
}
1551
- guard var currentInstallation = Self . current else {
1552
- let error = ParseError ( code: . unknownError,
1553
- message: " Current installation does not exist " )
1554
- callbackQueue. async {
1555
- completion ( . failure( error) )
1556
- }
1557
- return
1558
- }
1559
- guard currentInstallation. installationId != oldInstallationId else {
1560
- // If the installationId's are the same, assume successful migration already occured.
1561
- callbackQueue. async {
1562
- completion ( . success( currentInstallation) )
1563
- }
1564
- return
1565
- }
1566
- currentInstallation. installationId = oldInstallationId
1567
- currentInstallation. fetch ( options: options, callbackQueue: callbackQueue) { result in
1568
- switch result {
1569
- case . success( var updatedInstallation) :
1570
- if copyEntireInstallation {
1571
- updatedInstallation. updateAutomaticInfo ( )
1572
- Self . currentContainer. installationId = updatedInstallation. installationId
1573
- Self . currentContainer. currentInstallation = updatedInstallation
1574
- } else {
1575
- Self . current? . channels = updatedInstallation. channels
1576
- if Self . current? . deviceToken == nil {
1577
- Self . current? . deviceToken = updatedInstallation. deviceToken
1578
- }
1579
- }
1580
- Self . saveCurrentContainerToKeychain ( )
1581
- guard let latestInstallation = Self . current else {
1582
- let error = ParseError ( code: . unknownError,
1583
- message: " Had trouble migrating the installation " )
1584
- callbackQueue. async {
1585
- completion ( . failure( error) )
1586
- }
1587
- return
1588
- }
1589
- latestInstallation. save ( options: options,
1590
- callbackQueue: callbackQueue,
1591
- completion: completion)
1592
- case . failure( let error) :
1593
- callbackQueue. async {
1594
- completion ( . failure( error) )
1595
- }
1596
- }
1597
- }
1624
+ become ( oldInstallationId,
1625
+ copyEntireInstallation: copyEntireInstallation,
1626
+ completion: completion)
1598
1627
}
1599
1628
1600
1629
/**
@@ -1616,7 +1645,7 @@ public extension ParseInstallation {
1616
1645
callbackQueue: DispatchQueue = . main,
1617
1646
completion: @escaping ( Result < Void , ParseError > ) -> Void ) {
1618
1647
guard let objcParseKeychain = KeychainStore . objectiveC,
1619
- let oldInstallationId: String = objcParseKeychain. object ( forKey: " installationId " ) else {
1648
+ let oldInstallationId: String = objcParseKeychain. objectObjectiveC ( forKey: " installationId " ) else {
1620
1649
let error = ParseError ( code: . unknownError,
1621
1650
message: " Could not find Installation in the Objective-C SDK Keychain " )
1622
1651
callbackQueue. async {
0 commit comments