<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.7.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFRunLoopObserverCreateWithHandler(_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@F@CFRunLoopObserverCreateWithHandler"
  },
  "title" : "CFRunLoopObserverCreateWithHandler(_:_:_:_:_:)"
}
-->

# CFRunLoopObserverCreateWithHandler(_:_:_:_:_:)

Creates a CFRunLoopObserver object with a block-based handler.

```
func CFRunLoopObserverCreateWithHandler(_ allocator: CFAllocator!, _ activities: CFOptionFlags, _ repeats: Bool, _ order: CFIndex, _ block: ((CFRunLoopObserver?, CFRunLoopActivity) -> Void)!) -> CFRunLoopObserver!
```

## Parameters

`allocator`

The allocator to use to allocate memory for the new object. Pass `NULL` or [`kCFAllocatorDefault`](/documentation/CoreFoundation/kCFAllocatorDefault) to use the current default allocator.

`activities`

Set of flags identifying the activity stages of the run loop during which the observer is called. See [`CFRunLoopActivity`](/documentation/CoreFoundation/CFRunLoopActivity)for the list of stages. To have the observer called at multiple stages in the run loop, combine the [`CFRunLoopActivity`](/documentation/CoreFoundation/CFRunLoopActivity) values using the bitwise-OR operator.

`repeats`

A flag identifying whether the observer is called only once or every time through the run loop. If `repeats` is `false`, the observer is invalidated after it is called once, even if the observer was scheduled to be called at multiple stages within the run loop.

`order`

A priority index indicating the order in which run loop observers are processed. When multiple run loop observers are scheduled in the same activity stage in a given run loop mode, the observers are processed in increasing order of this parameter. Pass 0 unless there is a reason to do otherwise.

`block`

The block invoked when the observer runs. The block takes two arguments:

- `observer`: The run loop observer that is firing.
- `activity`: The current activity stage of the run loop.

## Return Value

The new CFRunLoopObserver object. Ownership follows the Create Rule described in [Ownership Policy](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148).

## Discussion

The run loop observer is not automatically added to a run loop. To add the observer to a run loop, use [`CFRunLoopAddObserver(_:_:_:)`](/documentation/CoreFoundation/CFRunLoopAddObserver(_:_:_:)). An observer can be registered to only one run loop, although it can be added to multiple run loop modes within that run loop.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)