簡體   English   中英

用於可轉換UILocalNotification的自定義核心數據訪問器

[英]Custom Core Data accessors for transformable UILocalNotification

我的一個實體上有一個可轉換的屬性,稱為提醒。 這是一個UILocalNotification。

現在,因為我想在添加它時安排它,並在刪除時取消它,我想覆蓋訪問器來處理調度和取消。

那怎么樣?

謝謝!

您實際上是在持久化UILocalNotification還是將其用作瞬態屬性?

我不會存儲它,而是將UILocalNotification作為屬性存儲為userInfo 您可以在該字典的鍵/值對中包含有關擁有實體的信息。 例如:

您可以在userInfo字典中為密鑰notificationID創建一個值,並將Core Data實體上的屬性notificationID設置為相同的值。 這樣,您只需在商店中存儲intNSString (這比可轉換更好)。

當您想再次獲取UILocalNotification時,您可以在您的實體類上創建一個訪問器,例如:

- (void)createNotification
{
    static NSUInteger kDeadlineWarningPeriod = 3600;
    UILocalNotification *notification = [[UILocalNotification alloc] init];

    …

    self.notificationID = @"some generated ID";

    [notification.userInfo setValue:self.notificationID forKey:@"notificationID"];

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    [notification release];
}

- (void)cancelNotification
{
    // We search for the notification.
    // The entity's ID will be stored in the notification's user info.

    [[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    UILocalNotification *notification = (UILocalNotification *)obj;
    NSDictionary *userInfo = notification.userInfo;

    NSString *notificationID = [userInfo valueForKey:@"notificationID"];

    if ([notificationID isEqualToString:self.notificationID])
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        *stop = YES;
                    self.notificationID = nil;
    }
     }];
}

當然,如果您確實需要訪問通知對象,則可以以相同的方式為通知創建訪問器。

希望能幫助到你。

UPDATE

所以,既然你有一個屬性,你可以調用提醒你的實體(我猜這是一個BOOL),它看起來像這樣:

// .h

@property (nonatomic, assign) BOOL reminder;

// .m

- (void)setReminder:(BOOL)reminder {

   [self willAccessValueForKey@"reminder"];
   BOOL hasReminder = [[self primitiveValueForKey:@"reminder"] booleanValue];
   [self didAccessValueForKey:@"reminder"];

   if (hasReminder && !reminder) {

        [self cancelNotification];
   }
   else if (!hasReminder && reminder) {

        [self createNotification];
   }

   if (reminder != hasReminder)
   {
        [self willChangeValueForKey:@"reminder"];
        [self setPrimitiveValue:[NSNumber numberWithBool:reminder] forKey@"reminder"];
        [self didChangeValueForKey:@"reminder"];
   }
}

實際上,您根本不需要存儲“提醒”屬性,只需檢查notificationID屬性是否為零即可。 這是我之前提出的建議。

我沒有檢查上面的代碼,但我在兩個項目中做了類似的事情。

請記住,如果您創建超過64個本地通知,則可能會遇到麻煩,因為您只能為每個應用創建多個通知。 因此,您可能希望在創建任何新的之前跟蹤您擁有的數量。

如果每個對象只有一個通知,則可以避免必須存儲notificationID,並且只需在Persistent存儲中使用NSManagedObject的objectId。

您可以使用以下代碼行序列化和反序列化objectId:

[[self.objectID URIRepresentation] absoluteString] 

[[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:[NSURL URLWithString:[localNotif.userInfo objectForKey: kYourReminderNotificationKey]

這是編輯的代碼:

- (void)createNotification
{        

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {

        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = self.dateDue;
        notif.timeZone = [NSTimeZone defaultTimeZone];


       notif.alertBody = @"Alert body";        

        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;


        NSDictionary *userDict = [NSDictionary dictionaryWithObject:[[self.objectID URIRepresentation] absoluteString] forKey:kRemindMeNotificationDataKey];


        notif.userInfo = userDict;

        [[UIApplication sharedApplication] scheduleLocalNotification:notif];

    }


}

- (void)cancelNotification
{

    [[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        UILocalNotification *notification = (UILocalNotification *)obj;
        NSDictionary *userInfo = notification.userInfo;

        NSString *notificationID = [userInfo valueForKey:kRemindMeNotificationDataKey];

        if ([notificationID isEqualToString:[[self.objectID URIRepresentation] absoluteString]])
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            *stop = YES;
        }
    }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM