Objective C: How to check for unread messages (number to be displayed as badge)
I have a tableView storing a list of notifications (retrieved from server). How can I check if the particular notification has been viewed by the user?
I was thinking of
1) storing the notification ids in NSUserdefaults and
2) check the retrieved notification list against the list in NSUserdefault at app launch,
3) if the notification does not exist in NSUserdefault, then notification == unread and badgeValueCount++.
However this s开发者_如何学JAVAeems highly inefficient and not scalable.
Can anyone advise a more efficient way of checking for unread items (and setting the badge with the unread count). Thanks!
This depends a lot on your system. For example, if the user might have several clients fetching the notifications and you want synchronization between them, the information will have to be stored server-side as part of the user data. If this is purely a client-side concern and the notifications are indexed by time, you can just store the last pull date and ask the server how many notifications are newer than that. Failing all of that, a list of notification IDs makes sense (though I would feel weird about putting it in the user defaults, just out of a sense of neatness and not wanting to bloat an app's defaults over time).
Why not simply storing your 'notifications' in some array ? You will be able to have the count of objects in the array, and also check wether an object is inside the array.
精彩评论