Browse files

Bug fix for XMPPRosterCoreDataStorage

  • Loading branch information...
1 parent f207d08 commit cc682107d25a28a231b7dc799e1c5302bc31fa42 @robbiehanson committed Aug 30, 2011
View
40 Extensions/CoreDataStorage/XMPPCoreDataStorage.m
@@ -305,24 +305,34 @@ - (void)setSaveThreshold:(NSUInteger)newSaveThreshold
- (XMPPJID *)myJIDForXMPPStream:(XMPPStream *)stream
{
- NSAssert(dispatch_get_current_queue() == storageQueue, @"Invoked on incorrect queue");
-
-
- NSNumber *key = [[NSNumber alloc] initWithPtr:stream];
+ __block XMPPJID *result = nil;
- XMPPJID *result = (XMPPJID *)[myJidCache objectForKey:key];
- if (!result)
- {
- result = [stream myJID];
+ dispatch_block_t block = ^{
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- if (result)
- [myJidCache setObject:result forKey:key];
- else
- [myJidCache removeObjectForKey:key];
- }
+ NSNumber *key = [NSNumber numberWithPtr:stream];
+
+ result = (XMPPJID *)[myJidCache objectForKey:key];
+ if (!result)
+ {
+ result = [stream myJID];
+
+ if (result)
+ {
+ [myJidCache setObject:result forKey:key];
+ }
+ }
+
+ [result retain];
+ [pool drain];
+ };
- [key release];
- return result;
+ if (dispatch_get_current_queue() == storageQueue)
+ block();
+ else
+ dispatch_sync(storageQueue, block);
+
+ return [result autorelease];
}
- (void)updateJidCache:(NSNotification *)notification
View
148 Extensions/Roster/CoreDataStorage/XMPPRosterCoreDataStorage.m
@@ -61,78 +61,16 @@ - (BOOL)configureWithParent:(XMPPRoster *)aParent queue:(dispatch_queue_t)queue
#pragma mark Utilities
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-- (XMPPUserCoreDataStorageObject *)_userForJID:(XMPPJID *)jid
- xmppStream:(XMPPStream *)stream
- managedObjectContext:(NSManagedObjectContext *)moc
-{
- XMPPLogTrace();
- AssertPrivateQueue();
-
- if (jid == nil) return nil;
-
- NSString *bareJIDStr = [jid bare];
-
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
- inManagedObjectContext:moc];
-
- NSPredicate *predicate;
- if (stream == nil)
- predicate = [NSPredicate predicateWithFormat:@"jidStr == %@", bareJIDStr];
- else
- predicate = [NSPredicate predicateWithFormat:@"jidStr == %@ AND streamBareJidStr == %@",
- bareJIDStr, [[self myJIDForXMPPStream:stream] bare]];
-
- NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
- [fetchRequest setEntity:entity];
- [fetchRequest setPredicate:predicate];
- [fetchRequest setIncludesPendingChanges:YES];
- [fetchRequest setFetchLimit:1];
-
- NSArray *results = [moc executeFetchRequest:fetchRequest error:nil];
-
- return (XMPPUserCoreDataStorageObject *)[results lastObject];
-}
-
-- (XMPPResourceCoreDataStorageObject *)_resourceForJID:(XMPPJID *)jid
- xmppStream:(XMPPStream *)stream
- managedObjectContext:(NSManagedObjectContext *)moc
+- (void)_clearAllResourcesForXMPPStream:(XMPPStream *)stream
{
XMPPLogTrace();
AssertPrivateQueue();
- if (jid == nil) return nil;
-
- NSString *fullJIDStr = [jid full];
+ NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPResourceCoreDataStorageObject"
inManagedObjectContext:moc];
- NSPredicate *predicate;
- if (stream == nil)
- predicate = [NSPredicate predicateWithFormat:@"jidStr == %@", fullJIDStr];
- else
- predicate = [NSPredicate predicateWithFormat:@"jidStr == %@ AND streamBareJidStr == %@",
- fullJIDStr, [[self myJIDForXMPPStream:stream] bare]];
-
- NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
- [fetchRequest setEntity:entity];
- [fetchRequest setPredicate:predicate];
- [fetchRequest setIncludesPendingChanges:YES];
- [fetchRequest setFetchLimit:1];
-
- NSArray *results = [moc executeFetchRequest:fetchRequest error:nil];
-
- return (XMPPResourceCoreDataStorageObject *)[results lastObject];
-}
-
-- (void)_clearAllResourcesForXMPPStream:(XMPPStream *)stream
-{
- XMPPLogTrace();
- AssertPrivateQueue();
-
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPResourceCoreDataStorageObject"
- inManagedObjectContext:[self managedObjectContext]];
-
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:saveThreshold];
@@ -146,13 +84,13 @@ - (void)_clearAllResourcesForXMPPStream:(XMPPStream *)stream
[fetchRequest setPredicate:predicate];
}
- NSArray *allResources = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil];
+ NSArray *allResources = [moc executeFetchRequest:fetchRequest error:nil];
NSUInteger unsavedCount = [self numberOfUnsavedChanges];
for (XMPPResourceCoreDataStorageObject *resource in allResources)
{
- [[self managedObjectContext] deleteObject:resource];
+ [moc deleteObject:resource];
if (++unsavedCount >= saveThreshold)
{
@@ -211,18 +149,13 @@ - (XMPPUserCoreDataStorageObject *)myUserForXMPPStream:(XMPPStream *)stream
XMPPLogTrace();
- if (moc == nil)
- {
- return nil;
- }
-
XMPPJID *myJID = stream.myJID;
if (myJID == nil)
{
return nil;
}
- return [self _userForJID:myJID xmppStream:stream managedObjectContext:moc];
+ return [self userForJID:myJID xmppStream:stream managedObjectContext:moc];
}
- (XMPPResourceCoreDataStorageObject *)myResourceForXMPPStream:(XMPPStream *)stream
@@ -232,18 +165,13 @@ - (XMPPResourceCoreDataStorageObject *)myResourceForXMPPStream:(XMPPStream *)str
XMPPLogTrace();
- if (moc == nil)
- {
- return nil;
- }
-
XMPPJID *myJID = stream.myJID;
if (myJID == nil)
{
return nil;
}
- return [self _resourceForJID:myJID xmppStream:stream managedObjectContext:moc];
+ return [self resourceForJID:myJID xmppStream:stream managedObjectContext:moc];
}
- (XMPPUserCoreDataStorageObject *)userForJID:(XMPPJID *)jid
@@ -254,12 +182,30 @@ - (XMPPUserCoreDataStorageObject *)userForJID:(XMPPJID *)jid
XMPPLogTrace();
- if (moc == nil)
- {
- return nil;
- }
+ if (jid == nil) return nil;
+ if (moc == nil) return nil;
+
+ NSString *bareJIDStr = [jid bare];
+
+ NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
+ inManagedObjectContext:moc];
+
+ NSPredicate *predicate;
+ if (stream == nil)
+ predicate = [NSPredicate predicateWithFormat:@"jidStr == %@", bareJIDStr];
+ else
+ predicate = [NSPredicate predicateWithFormat:@"jidStr == %@ AND streamBareJidStr == %@",
+ bareJIDStr, [[self myJIDForXMPPStream:stream] bare]];
+
+ NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
+ [fetchRequest setEntity:entity];
+ [fetchRequest setPredicate:predicate];
+ [fetchRequest setIncludesPendingChanges:YES];
+ [fetchRequest setFetchLimit:1];
+
+ NSArray *results = [moc executeFetchRequest:fetchRequest error:nil];
- return [self _userForJID:jid xmppStream:stream managedObjectContext:moc];
+ return (XMPPUserCoreDataStorageObject *)[results lastObject];
}
- (XMPPResourceCoreDataStorageObject *)resourceForJID:(XMPPJID *)jid
@@ -270,12 +216,30 @@ - (XMPPResourceCoreDataStorageObject *)resourceForJID:(XMPPJID *)jid
XMPPLogTrace();
- if (moc == nil)
- {
- return nil;
- }
+ if (jid == nil) return nil;
+ if (moc == nil) return nil;
- return [self _resourceForJID:jid xmppStream:stream managedObjectContext:moc];
+ NSString *fullJIDStr = [jid full];
+
+ NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPResourceCoreDataStorageObject"
+ inManagedObjectContext:moc];
+
+ NSPredicate *predicate;
+ if (stream == nil)
+ predicate = [NSPredicate predicateWithFormat:@"jidStr == %@", fullJIDStr];
+ else
+ predicate = [NSPredicate predicateWithFormat:@"jidStr == %@ AND streamBareJidStr == %@",
+ fullJIDStr, [[self myJIDForXMPPStream:stream] bare]];
+
+ NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
+ [fetchRequest setEntity:entity];
+ [fetchRequest setPredicate:predicate];
+ [fetchRequest setIncludesPendingChanges:YES];
+ [fetchRequest setFetchLimit:1];
+
+ NSArray *results = [moc executeFetchRequest:fetchRequest error:nil];
+
+ return (XMPPResourceCoreDataStorageObject *)[results lastObject];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -359,7 +323,7 @@ - (void)handleRosterItem:(NSXMLElement *)itemSubElement xmppStream:(XMPPStream *
NSString *jidStr = [item attributeStringValueForName:@"jid"];
XMPPJID *jid = [[XMPPJID jidWithString:jidStr] bareJID];
- XMPPUserCoreDataStorageObject *user = [self _userForJID:jid xmppStream:stream managedObjectContext:moc];
+ XMPPUserCoreDataStorageObject *user = [self userForJID:jid xmppStream:stream managedObjectContext:moc];
NSString *subscription = [item attributeStringValueForName:@"subscription"];
if ([subscription isEqualToString:@"remove"])
@@ -397,7 +361,7 @@ - (void)handlePresence:(XMPPPresence *)presence xmppStream:(XMPPStream *)stream
XMPPJID *jid = [presence from];
NSManagedObjectContext *moc = [self managedObjectContext];
- XMPPUserCoreDataStorageObject *user = [self _userForJID:jid xmppStream:stream managedObjectContext:moc];
+ XMPPUserCoreDataStorageObject *user = [self userForJID:jid xmppStream:stream managedObjectContext:moc];
if (user)
{
@@ -417,7 +381,7 @@ - (BOOL)userExistsWithJID:(XMPPJID *)jid xmppStream:(XMPPStream *)stream
[self executeBlock:^{
NSManagedObjectContext *moc = [self managedObjectContext];
- XMPPUserCoreDataStorageObject *user = [self _userForJID:jid xmppStream:stream managedObjectContext:moc];
+ XMPPUserCoreDataStorageObject *user = [self userForJID:jid xmppStream:stream managedObjectContext:moc];
result = (user != nil);
}];
@@ -436,7 +400,7 @@ - (void)setPhoto:(NSImage *)photo forUserWithJID:(XMPPJID *)jid xmppStream:(XMPP
[self scheduleBlock:^{
NSManagedObjectContext *moc = [self managedObjectContext];
- XMPPUserCoreDataStorageObject *user = [self _userForJID:jid xmppStream:stream managedObjectContext:moc];
+ XMPPUserCoreDataStorageObject *user = [self userForJID:jid xmppStream:stream managedObjectContext:moc];
if (user)
{

0 comments on commit cc68210

Please sign in to comment.