objective c - CoreData ascending:YES, show latest from the bottom -


i have list of messages in core data store(100 messages).

nssortdescriptor * sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"created" ascending:yes]; nsarray *sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors]; [fetchrequest setfetchlimit:20]; 

it shows first 20 messages in order need. need is: show last 20 messages, sorted date of creation, should appeared form bottom of tableview.

storage:  1 : test 1 2 : test 2 3 : test 3  shows :  1 : test 1 2 : test 2  **supposed show in table view:**  3 : test 3 2 : test 2 1 : test 1 

fetchedresultscontroller:

- (nsfetchedresultscontroller *)fetchedresultscontroller{      if (_fetchedresultscontroller != nil) {         return _fetchedresultscontroller;     }      nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init];     nsentitydescription *entity = [nsentitydescription entityforname:@"messages" inmanagedobjectcontext:self.managedobjectcontext];     [fetchrequest setentity:entity];     [fetchrequest setfetchlimit:20];      nssortdescriptor * sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"created" ascending:yes];     nsarray *sortdescriptors = @[sortdescriptor];     [fetchrequest setsortdescriptors:sortdescriptors];      nspredicate *predicate = [nspredicate predicatewithformat:@"(owner = %@) , (stream == %@) , (topic == %@)", ownerguid,openstream,currenttopic];     [fetchrequest setpredicate:predicate];      nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:nil cachename:nil];     afetchedresultscontroller.delegate = self;      self.fetchedresultscontroller = afetchedresultscontroller;     self.fetchedresultscontroller.delegate = self;      nserror *error = nil;     if (![self.fetchedresultscontroller performfetch:&error]) {         nslog(@"unresolved error %@, %@", error, [error userinfo]);         abort();     }     [self.fetchedresultscontroller performfetch:null];      return _fetchedresultscontroller; } 

all should need change sortdescriptor use ascending:no last 20 messages.

nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"created" ascending:no]; 

now, you've got right messages, they're backwards in array. can reverse array using following code.

nsarray *messages = [[[fetchedmessages] reverseobjectenumerator] allobjects]; 

edit:

since using nsfetchedresultscontroller solution require lot of additional code work.

the easiest solution can think of implement custom method getting objects out of fetched results controller.

- (id)objectatindexpath:(nsindexpath *)indexpath {     nsinteger sectioncount = [self.tableview numberofrowsinsection:indexpath.section];     nsinteger newrowindex = sectioncount - indexpath.row - 1;     nsindexpath *newindexpath = [nsindexpath indexpathforrow:newrowindex insection:indexpath.section];      return [self.fetchedresultscontroller objectatindexpath:newindexpath]; } 

then replace

[self.fetchedresultscontroller objectatindexpath:indexpath]; 

with

[self objectatindexpath:indexpath]; 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -