@@ -254,22 +254,43 @@ private final class CloudKitLiveObserver: Logging {
254
254
private func store( _ records: [ CKRecord ] ) {
255
255
log. debug ( " Storing live records " )
256
256
257
- storage. backgroundUpdate { realm in
257
+ storage. backgroundUpdate { [ weak self] realm in
258
+ guard let self else { return }
259
+
258
260
records. forEach { record in
259
261
guard let asset = SessionAsset ( record: record) else { return }
260
262
guard let session = realm. object ( ofType: Session . self, forPrimaryKey: asset. sessionId) else { return }
261
263
guard let instance = session. instances. first else { return }
262
264
265
+ /// Allow record to override state from the backend API only if the record's `overrideState` is set to `1`,
266
+ /// otherwise existing local data from Apple's backend always wins.
267
+ let canOverrideExistingState = record [ " overrideState " ] as? Int == 1
268
+
263
269
if let existingAsset = realm. object ( ofType: SessionAsset . self, forPrimaryKey: asset. identifier) {
264
- // update existing asset hls URL if appropriate
265
- existingAsset. remoteURL = asset. remoteURL
270
+ /// Update existing asset hls URL if appropriate
271
+ if canOverrideExistingState {
272
+ existingAsset. remoteURL = asset. remoteURL
273
+ } else {
274
+ self . log. info ( " Ignoring remoteURL override from record for \( asset. sessionId, privacy: . public) because overrideState is not 1 " )
275
+ }
266
276
} else {
267
277
// add new live asset to corresponding session
268
278
session. assets. append ( asset)
269
279
}
270
280
271
281
instance. isForcedLive = ( record [ " isLive " ] as? Int == 1 )
272
- instance. isCurrentlyLive = instance. isForcedLive
282
+
283
+ /// Allow record to override live state from not live to live, but not the other way around.
284
+ if !instance. isCurrentlyLive {
285
+ instance. isCurrentlyLive = instance. isForcedLive
286
+ } else {
287
+ /// Allow record to override live state however it wants if record's `overrideState` is `1`.
288
+ if canOverrideExistingState {
289
+ instance. isCurrentlyLive = instance. isForcedLive
290
+ } else {
291
+ self . log. info ( " Ignoring live state override from record for \( asset. sessionId, privacy: . public) because overrideState is not 1 " )
292
+ }
293
+ }
273
294
}
274
295
}
275
296
}
0 commit comments