开发者

Sorting and Setting Default in CoreData List

My iOS application uses CoreData to store many things, and NSUserDefaults to store other things that CoreData would be overkill for.

I'm going to simplify this so bear with me (Note: for the sake of this example let's use credit cards). When my application loads it displays a list of saved credit cards.开发者_C百科 Now, when each card is added it is assigned a number 1-10 (let's call it cardNum) that stays with the card for the life of it.

So we add one card and assign it #1. We add another card and assign it #2. Another... #3. We can set a default card that will be highlighted on launch by tapping it. What it does is saves the cardNum as the cDefault to NSUserDefaults to keep track of it (effectively setting card X as the default card).

(crude drawing of a table view)

//cDefault is 2
------------
1 - Card1
------------
2 - Card2 (cDefault)
------------
3 - Card3
------------
4 - Card4
------------

So let's say we assign cDefault to 2 and then next week we delete that card. On deletion, I need to automatically assign one of the other cards to be a default card.

(crude drawing of table view AFTER card 2 is deleted - note how cDefault is still set...)

//cDefault is still 2
------------
1 - Card1
------------
3 - Card3
------------
4 - Card4
------------

So my question is how, upon deletion, to set another card as cDefault (i.e if it was an array I would probably set the new default to the cDefault value of whatever card is at indexPath.row==0 but I'm not sure if I can do that using CoreData)

Please ask as many questions as you need to. I tried to explain this to the best of my abilities but if I was unclear on anything please ask me.

Thank you so much in advance. James


First, I don't understand why you have to separate certain informations from your managed objects... I would define a BOOL property like "highlighted" in the Card class and handle all your stuff in core data... it sounds more OOP to me :P Second, I didn't try it before, but you can subscribe to the NSNotificationCenter and listen to NSManagedObjectContextDidSaveNotification, that notification contains an userInfo dictionary with a key named NSDeletedObjectsKey that should contains a set of deleted objects (cards), in your selector you can write the piece of logic to update the objects in the store (that is, set the "highlighted" property to YES for the first card for example)... this approach is cool because can be placed anywhere in your code, anyway the simplest and obvious place to handle the update is where you are deleting the card.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜