ios - Effectively saving using MagicalRecord -


i using magicalrecord in ios app, , not sure right way save.

my app chatting, , every time logged in user received message, need update time token.

now this:

[magicalrecord savewithblock:^(nsmanagedobjectcontext *localcontext) {

    user *currentuser = [user mr_findfirstbyattribute:@"id"                                              withvalue:@(_current_user_id)                                              incontext:localcontext];      currentuser.lastchatmessagetimetoken = [nsdate date];  }]; 

however, believe not efficient because once user logged in, id determined, , currentuser same.

i thinking should cache currentuser instance variable, cannot find corresponding magicalrecord method perform.

also, told not cache nsmanagedobject because bind context.

so not sure should do. help?

you correct in fetching, updating, , saving single object not efficient. can hold on reference of currentuser object long know context belongs. there, code this;

currentuser = //from somewhere else, property perhaps [magicalrecord savewithblock:^(nsmanagedobjectcontext *localcontext){     user *localuser = [currentuser mr_incontext:localcontext];     localuser.lastchatmessagetime = [nsdate date]; }]; 

fetching object object id going faster performing fetch request. however, since you're changing 1 object, recommend saving on current thread (as long context on correct thread):

nsmanagedobjectcontext *context = //.... user *currentuser = //from somewhere, , create in context  currentuser.lastchatmessagetime = [nsdate date];  [context mr_savetopersistentstoreandwait]; 

this way there no blocks deal with, , don't have perform save on background queue since single record update fast enough on main thread.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -