共用方式為


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 ();

適用於