NSManagedObjectContext.Notifications.ObserveObjectsDidChange 方法

定义

重载

ObserveObjectsDidChange(EventHandler<NSManagedObjectChangeEventArgs>)

常量的强类型通知 ObjectsDidChangeNotification

ObserveObjectsDidChange(NSObject, EventHandler<NSManagedObjectChangeEventArgs>)

常量的强类型通知 ObjectsDidChangeNotification

ObserveObjectsDidChange(EventHandler<NSManagedObjectChangeEventArgs>)

常量的强类型通知 ObjectsDidChangeNotification

public static Foundation.NSObject ObserveObjectsDidChange (EventHandler<CoreData.NSManagedObjectChangeEventArgs> handler);
static member ObserveObjectsDidChange : EventHandler<CoreData.NSManagedObjectChangeEventArgs> -> Foundation.NSObject

参数

handler
EventHandler<NSManagedObjectChangeEventArgs>

发布通知时调用的方法。

返回

令牌对象,可用于停止接收通知,方法是释放通知或将其传递给 RemoveObservers(IEnumerable<NSObject>)

注解

以下示例演示开发人员如何在代码中使用此方法:

//
// Lambda style
//

// listening
notification = NSManagedObjectContext.Notifications.ObserveObjectsDidChange ((sender, args) => {
    /* Access strongly typed args */
    Console.WriteLine ("Notification: {0}", args.Notification);

    Console.WriteLine ("InsertedObjects", args.InsertedObjects);
    Console.WriteLine ("UpdatedObjects", args.UpdatedObjects);
    Console.WriteLine ("DeletedObjects", args.DeletedObjects);
    Console.WriteLine ("RefreshedObjects", args.RefreshedObjects);
    Console.WriteLine ("InvalidatedObjects", args.InvalidatedObjects);
    Console.WriteLine ("InvalidatedAllObjects", args.InvalidatedAllObjects);
});

// To stop listening:
notification.Dispose ();

//
//Method style
//
NSObject notification;
void Callback (object sender, CoreData.NSManagedObjectChangeEventArgs args)
{
    // Access strongly typed args
    Console.WriteLine ("Notification: {0}", args.Notification);

    Console.WriteLine ("InsertedObjects", args.InsertedObjects);
    Console.WriteLine ("UpdatedObjects", args.UpdatedObjects);
    Console.WriteLine ("DeletedObjects", args.DeletedObjects);
    Console.WriteLine ("RefreshedObjects", args.RefreshedObjects);
    Console.WriteLine ("InvalidatedObjects", args.InvalidatedObjects);
    Console.WriteLine ("InvalidatedAllObjects", args.InvalidatedAllObjects);
}

void Setup ()
{
    notification = NSManagedObjectContext.Notifications.ObserveObjectsDidChange (Callback);
}

void Teardown ()
{
    notification.Dispose ();
}

适用于

ObserveObjectsDidChange(NSObject, EventHandler<NSManagedObjectChangeEventArgs>)

常量的强类型通知 ObjectsDidChangeNotification

public static Foundation.NSObject ObserveObjectsDidChange (Foundation.NSObject objectToObserve, EventHandler<CoreData.NSManagedObjectChangeEventArgs> handler);
static member ObserveObjectsDidChange : Foundation.NSObject * EventHandler<CoreData.NSManagedObjectChangeEventArgs> -> Foundation.NSObject

参数

objectToObserve
NSObject

要观察的对象。

handler
EventHandler<NSManagedObjectChangeEventArgs>

发布通知时调用的方法。

返回

令牌对象,可用于停止接收通知,方法是释放通知或将其传递给 RemoveObservers(IEnumerable<NSObject>)

注解

此方法可用于订阅 ObjectsDidChangeNotification 通知。

// Listen to all notifications posted for any object
var token = NSManagedObjectContext.Notifications.ObserveObjectsDidChange ((notification) => {
	Console.WriteLine ("Observed ObjectsDidChangeNotification!");
};

// Listen to all notifications posted for a single object
var token = NSManagedObjectContext.Notifications.ObserveObjectsDidChange (objectToObserve, (notification) => {
	Console.WriteLine ($"Observed ObjectsDidChangeNotification for {nameof (objectToObserve)}!");
};

// Stop listening for notifications
token.Dispose ();

适用于