NSManagedObjectContext.ObjectsDidChangeNotification 属性

定义

ObjectsDidChange 的通知常量

[Foundation.Advice("Use NSManagedObjectContext.Notifications.ObserveObjectsDidChange helper method instead.")]
[Foundation.Field("NSManagedObjectContextObjectsDidChangeNotification", "CoreData")]
public static Foundation.NSString ObjectsDidChangeNotification { get; }
member this.ObjectsDidChangeNotification : Foundation.NSString

属性值

NSString 常量 应用作 NSNotificationCenter 的标记。

属性

注解

此常量可用于注册 NSNotificationCenter 此通知的侦听器。 这是 NSString 而不是字符串,因为这些值可以用作某些本机库中的标记,而不是纯粹用于其实际字符串内容。 回调的“notification”参数包含特定于通知类型的额外信息。

若要订阅此通知,开发人员可以使用便捷 NSManagedObjectContext.Notifications的 .ObserveObjectsDidChange 方法,该方法提供对通知参数的强类型访问。

以下示例演示如何使用强类型 Notifications 类来排除通知中可用属性的猜测:

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

以下示例演示如何将通知与 DefaultCenter API 配合使用:

// Lambda style
NSNotificationCenter.DefaultCenter.AddObserver (
        NSManagedObjectContext.ObjectsDidChangeNotification, (notification) => {Console.WriteLine ("Received the notification NSManagedObjectContext", notification); }


// Method style
void Callback (NSNotification notification)
{
    Console.WriteLine ("Received a notification NSManagedObjectContext", notification);
}

void Setup ()
{
    NSNotificationCenter.DefaultCenter.AddObserver (NSManagedObjectContext.ObjectsDidChangeNotification, Callback);
}

适用于