alionthego
alionthego

Reputation: 9743

using core data, is it possible to change the value of an entity's attribute during a fetch?

I am using coreData in a chat app. I want to set each messages messageViewed bool attribute to true anytime I fetch all messages in a chat room.

Of course I could do this by first fetching all the messages in a room and then iterating through each message and setting the messageViewed attribute to true, however I am looking for a more efficient way of achieving this.

I remember reading somewhere that it may be possible during the fetch to define or change the value of an entity's attribute for all items fetched directly in the fetch request but I can't remember where I read that or how to implement it.

Upvotes: 0

Views: 85

Answers (1)

fewlinesofcode
fewlinesofcode

Reputation: 3082

awakeFromFetch is method you're talking about.

But be careful about it's behaviour:

The managed object context’s change processing is explicitly disabled around this method so that you can use public setters to establish transient values and other caches without dirtying the object or its context. Because of this, however, you should not modify relationships in this method as the inverse will not be set.

Upvotes: 1

Related Questions