Should I get all Core Data objects on iPhone?

I am creating a social application that has a chatView. Message is an entity. In userViewController (rootViewController) I want to simply display the number of unread messages from each user. But in chatView I want to show messages sent between me and the selected user.

How do I get messages from Core Data?

  • Is there a way to get them all in an NSMutableDictionary with userIds as keys, each of which points to NSMutableArray objects for messages? Or would I need to get all the messages and then iterate over them using Objective-C to arrange them as described?

  • Anyway, should I load them all in appDelegate? Or should I download messages only for a specific conversation while viewing the corresponding chatView?

  • To get unread counts, should I iterate over the received message objects to make the NSMutableDictionary userID keys pointing to count values? Or should I make a predicate that acts like a SQL sum query grouped by userId (is this possible with Core Data?)?

  • Should I create a model, name it UnreadMessageCount, which stores the number of unread messages that each user has, and a link to the model User? I want to have all unread messages on the first page of the application, so this will speed up the request. But receiving messages will be slower, because I will not need to add it only to the Message model, but also I will have to increase the counter for this user in the UnreadMessageCount model. In addition, the data will be duplicated. In addition, I will only make a request for all messages to be counted once per application launch, but I will make requests to update the number of unread messages every time I receive a new message, and I am not on the chatView page with this user.

, iPhone Core Data. , , , .

!

Matt

+3
2

, , , , :

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:context];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@", user];
[request setPredicate:predicate];

NSInteger count = [context countForFetchRequest:request error:nil];

, Message user.

, :

NSArray *messages = [context executeFetchRequest:request error:nil];
+3

iPhone , CoreData ( , ). , , , :

  • , , messages user
  • NSFetchedResultsController , .
  • , , , . .
0

Source: https://habr.com/ru/post/1762986/


All Articles