# Lifecycle events > **Note** > > **Building with LiveObjects?** Help shape its future by [sharing your use case](https://44qpp.share.hsforms.com/2fZobHQA1ToyRfB9xqZYQmQ). ## Handle synchronization events The [channel.object](https://ably.com/docs/liveobjects/concepts/objects.md#channel-object)[channel.object](https://ably.com/docs/liveobjects/concepts/objects.md#channel-object) instance emits synchronization events that indicate when the local state on the client is being synchronized with the Ably service. These events can be useful for displaying loading indicators, preventing user interactions during synchronization, or triggering application logic when data is first loaded. | Event | Description | | ----- | ----------- | | syncing | Emitted when the local copy of objects on a channel begins synchronizing with the Ably service. | | synced | Emitted when the local copy of objects on a channel has been synchronized with the Ably service. | ### Javascript ``` channel.object.on('syncing', () => { console.log('Objects are syncing...'); // Show a loading indicator and disable edits in the application }); channel.object.on('synced', () => { console.log('Objects have been synced.'); // Hide loading indicator }); ``` ### Swift ``` channel.object.on(event: .syncing) { print("Objects are syncing...") // Show a loading indicator and disable edits in the application } channel.object.on(event: .synced) { print("Objects have been synced.") // Hide loading indicator } ``` ### Java ``` channel.object.on(ObjectStateEvent.SYNCING, stateEvent -> { System.out.println("Objects are syncing..."); // Show a loading indicator and disable edits in the application }); channel.object.on(ObjectStateEvent.SYNCED, stateEvent -> { System.out.println("Objects have been synced."); // Hide loading indicator }); ``` ### Python ``` from ably.pubsub.server.liveobjects import ObjectsEvent def on_syncing(): print('Objects are syncing...') # Show a loading indicator and disable edits in the application def on_synced(): print('Objects have been synced.') # Hide loading indicator syncing_subscription = channel.object.on(ObjectsEvent.SYNCING, on_syncing) synced_subscription = channel.object.on(ObjectsEvent.SYNCED, on_synced) ``` The `on(...)` method returns a `Subscription` object. Keep a reference to it and call `subscription.unsubscribe()` when you no longer want to receive these events; calling it more than once has no effect. To remove all listeners at once, call `channel.object.offAll()`. The `ObjectStateEvent` enum is in the `io.ably.lib.liveobjects.state` package. The `on(event:)` method returns an `any StatusSubscription`. Keep a reference to it and call `subscription.off()` when you no longer want to receive these events. There is no bulk removal method; remove each subscription individually through its own `off()`. The `on()` method calls the callback with no arguments, and returns a `StatusSubscription`. Keep a reference to it and call `subscription.off()` when you no longer want to receive these events. Alternatively, call `channel.object.off(event, callback)` to remove every registration of that callback for the event. `ObjectsEvent` is imported from `ably.pubsub.server.liveobjects`. > **Important** > > LiveObjects synchronization events do not replace [connection](https://ably.com/docs/connect/states.md) or [channel](https://ably.com/docs/channels/states.md) states. You should still monitor these states and handle [connection](https://ably.com/docs/connect/states.md#handling-failures) and [channel](https://ably.com/docs/channels/states.md#failure) failures to ensure your application behaves as expected. LiveObjects synchronization events specifically inform you about the progress of LiveObjects data synchronization and should be used alongside other state management mechanisms. ## Handle object lifecycle events ### Detect object deletion Objects can become unreachable when they are removed from the object tree using [`remove()`](https://ably.com/docs/liveobjects/map.md#remove) and never reassigned. Unreachable objects are garbage collected by Ably, typically after 24 hours. Since deleted objects are unreachable via any path, `object.delete` events can only be received through [Instance](https://ably.com/docs/liveobjects/concepts/instance.md) subscriptions, which track a specific object by its ID regardless of its location in the object tree: #### Javascript ``` const counterInstance = rootObject.get('visits').instance(); if (counterInstance) { counterInstance.subscribe(({ object, message }) => { if (message?.operation.action === 'object.delete') { console.log('Counter instance was deleted'); // The client automatically unsubscribes the subscription after this notification // Remove references to this object from your application } else { console.log('Counter updated:', object.value()); } }); } ``` #### Swift ``` // rootObject obtained from channel.object.get() if case .liveCounter(let counterInstance)? = try rootObject.get(key: "visits").instance() { try counterInstance.subscribe { event in if event.message?.operation.action == .objectDelete { print("Counter instance was deleted") // The client automatically unsubscribes the subscription after this notification // Remove references to this object from your application } else if let value = try? counterInstance.value { print("Counter updated: \(value)") } } } ``` #### Java ``` // rootObject obtained from channel.object.get() Instance visitsInstance = rootObject.get("visits").instance(); if (visitsInstance != null) { LiveCounterInstance counterInstance = visitsInstance.asLiveCounter(); counterInstance.subscribe(event -> { ObjectMessage message = event.getMessage(); if (message != null && message.getOperation().getAction() == ObjectOperationAction.OBJECT_DELETE) { System.out.println("Counter instance was deleted"); // The client automatically unsubscribes the subscription after this notification // Remove references to this object from your application } else { System.out.println("Counter updated: " + counterInstance.value()); } }); } ``` #### Python ``` from ably.pubsub.server.liveobjects import ObjectOperationAction # root_object obtained from channel.object.get() visits_instance = root_object.get('visits').instance() if visits_instance is not None: counter_instance = visits_instance.as_live_counter() def on_update(event): if event.message is not None and event.message.operation.action == ObjectOperationAction.OBJECT_DELETE: print('Counter instance was deleted') # The client automatically unsubscribes the subscription after this notification # Remove references to this object from your application else: print('Counter updated:', counter_instance.value()) counter_instance.subscribe(on_update) ``` In most cases, you don't need to explicitly handle object deletion. Your application should have already reacted to object removal when a corresponding [`LiveMap.remove()`](https://ably.com/docs/liveobjects/map.md#remove) operation was received. However, if your application separately stores references to object instances and does not properly clear them when objects are orphaned, any later interactions with those objects after they are deleted will result in an error. In such cases, handling deletion events via Instance subscriptions helps ensure that those references are cleaned up and runtime errors are avoided. > **Note** > > The [channel object](https://ably.com/docs/liveobjects/concepts/objects.md#channel-object) cannot be deleted, so deletion notifications will never occur for the channel object itself. ## Related Topics - [Batch operations](https://ably.com/docs/liveobjects/batch.md): Group multiple objects operations into a single channel message to apply grouped operations atomically and improve performance. - [Typing](https://ably.com/docs/liveobjects/typing.md): Type objects on a channel for type safety and code autocompletion. - [Inband objects](https://ably.com/docs/liveobjects/inband-objects.md): Subscribe to LiveObjects updates from Pub/Sub SDKs. - [Object storage](https://ably.com/docs/liveobjects/storage.md): Learn about LiveObjects object storage. - [Using the REST SDK](https://ably.com/docs/liveobjects/rest-sdk-usage.md): Learn how to work with Ably LiveObjects using the REST SDK - [Using the REST API](https://ably.com/docs/liveobjects/rest-api-usage.md): Learn how to work with Ably LiveObjects using the REST API ## Documentation Index To discover additional Ably documentation: 1. Fetch [llms.txt](https://ably.com/llms.txt) for the canonical list of available pages. 2. Identify relevant URLs from that index. 3. Fetch target pages as needed. Avoid using assumed or outdated documentation paths.