Frage

I am making chat-app and I looking the way to save and load from core data. I save and load to it all user's history and it works good.

I am looking the way how can I load and save roster list

I am not sure here. I load user's info from web at startup by getting user's ids from roster list and request web service for that user's info. I want to save it to core data with roster list.

How can I set for every jUser (loaded from core data) his web server info? There are 2 problems here:

  1. I can not get JUser from core data for its id
  2. If I do 1. I can set to that user his web image and data to his core data's storied account. - I think it is not a good idea. How can I manage users here?

Some code:

- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController == nil)
{
    NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                              inManagedObjectContext: moc];

    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
    NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects: sd1, sd2, nil];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setFetchBatchSize:10];

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                    managedObjectContext:moc
                                                                      sectionNameKeyPath:@"sectionNum"
                                                                               cacheName:nil];
    [_fetchedResultsController setDelegate:self];


    NSError *error = nil;
    if (![_fetchedResultsController performFetch:&error])
    {
        //DDLogError(@"Error performing fetch: %@", error);
    }

}

return _fetchedResultsController;
}
War es hilfreich?

Lösung

What you are showing above is for keeping track/getting the info that is populated with a request to the XMPP server for it's roster (be it an autoFetch or a manual fetch using the XMPPRoster 'fetchRoster' method (assuming you have set the CoreData way of storing your roster data, not memory).

Once the response to the roster fetch is returned (iq result), the delegates within the XMPPRoster instance will capture and put in place with the given storage option. If the server you are using conforms to the XMPP rfc, then this should happen pretty automatically on the return.

For example - in XMPPRoster.didReceiveIQ() - you can see the call to '[xmppStorage handleRosterItem:item xmppStream:xmppStream]'. This is where the processed

You can extend the storage here (XMPPRosterCoreDataStorage and XMPPUserCoreDataStorage for example) and set in place to add additional information to the entity. For example here - XMPPUserCoreDataStorage has an over-ride '(void)didUpdateWithItem:(NSXMLElement *)item' that you can define attributes in to point to another entity. Here you would copy the existing data model and add your own attributes to it - using the over-ride above to enter them.

As for the messages, depends on if a MUC or a 1:1 - but they use different managed objects as well. XEP-0045 is what is storing the MUC messages that you can try to attach to for the users last message in there - as well as XMPPMessageArchiving for the 1:1 storage, but you would still need support from the server on this if you need to persist the capturing of another users last message - unless you are only talking about per session (which you could then store locally for display).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top