问题
I'm not sure how this would work, but what I'd like is something similar to Apple's alarm clock that comes with the iPhone. It basically just lets you pick a time of an alarm, name the alarm, and then you can choose how often you want it to repeat (Sunday - Saturday). Based on what you choose, the alarm fires once, or at a repeated interval.
In my Core Data model, I wasn't sure how to model that. If I were thinking in terms of just plain old objects, I would think I would have some alarm object, and one of its properties would be an array. In that array I could have the day values of Sunday-Sautrday. Then when a new alarm object is created, I would schedule a UILocalNotification for the time selected, and the days chosen. To model that in terms of database objects, I'm not sure what I'm supposed to do. I was thinking something like:
Alarm - (name/string) Day - (Sunday - Saturday/represented by integers 0-6, 1 to many relationship from Alarm to Day)
Assuming that is ok in the database, then I'm not sure how I should go about scheduling the UILocalNotifications since I thought you could only have 64 per app. I'm thinking that I could have some mechanism to schedule the first 64 alarms possible, then when the app is opened, it would just reschedule the next upcoming 64 events. Is that how I would do that? Thanks.
回答1:
Using 2 entities is overkill. I would just have the Alarm
entity and have a single integer attribute on it to hold the alarm days. Outside of the entity, I would have an enumeration which defines how the alarm days number is interpreted. Something like:
typedef AlarmDays {
Monday = 0,
Tuesday = 1 << 0,
Wednesday = 1 << 1,
Thursday = 1 << 2,
Friday = 1 << 3,
Saturday = 1 << 4,
Sunday = 1 << 5
} AlarmDays;
Then you can test which days it should be on using:
if (alarm.alarmDays & Monday) {
// the alarm should fire on mondays
}
And you can use the features of UILocalNotification
, such as repeatInterval
so you don't need to explicitly add gazillions of notifications to the system.
来源:https://stackoverflow.com/questions/17310935/modeling-a-repeating-event-in-a-database-and-using-uilocalnotification-to-fire