robbiehanson / XMPPFramework

An XMPP Framework in Objective-C for Mac and iOS
Other
5.91k stars 2.09k forks source link

Add _Nullable to XMPPRoster #1160

Open alessioarsuffi opened 4 years ago

alessioarsuffi commented 4 years ago

Add _Nullable to method of XMPPRoster:

(NSArray *)jidsForXMPPStream:(XMPPStream * _Nullable)stream

to be able to call this from swift with a nil stream reference.

This is especially useful when user wants to retrieve roster from swift code and stream is not yet connected.

alessioarsuffi commented 4 years ago

Hi @chrisballinger :)

This is a small change to better support swift. Please let me know if this is ok for you, i'm available to add more _Nullable flags to the framework in future.

Thanks

chrisballinger commented 4 years ago

I'm not sure if we really want to change this API contract. You can always construct an XMPPStream that is not connected and pass that in.

alessioarsuffi commented 4 years ago
- (NSArray *)jidsForXMPPStream:(XMPPStream * _Nullable)stream {

    XMPPLogTrace();

    __block NSMutableArray *results = [NSMutableArray array];

    [self executeBlock:^{

        NSManagedObjectContext *moc = [self managedObjectContext];

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

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entity];
        [fetchRequest setFetchBatchSize:self->saveThreshold];

        if (stream)
        {
            NSPredicate *predicate;
            predicate = [NSPredicate predicateWithFormat:@"streamBareJidStr == %@",
                         [[self myJIDForXMPPStream:stream] bare]];

            [fetchRequest setPredicate:predicate];
        }

        NSArray *allUsers = [moc executeFetchRequest:fetchRequest error:nil];

        for(XMPPUserCoreDataStorageObject *user in allUsers){
            [results addObject:[user.jid bareJID]];
        }

    }];

    return results;
}

The problem is inside this method, if we use a not connected stream, will be applied to predicate a nil streamBareJidStr value, resulting in 0 fetched jabbers from database, if we pass a stream which is nil and not connected we will fetch jabbers from roster even if we are still not connected. Without _Nullable in that signature is not possible to fetch jabbers from database without a valid stream connection. Because query will exclude all jabbers in roster.

Also, if stream cannot be nil, why we check if stream exist to create a predicate?

chrisballinger commented 3 years ago

@alessioarsuffi Sorry for the delay on this! An XMPPStream can be instantiated without being connected