Skip to content

Commit

Permalink
Explicitly obtain permanent objectIDs every time
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Feb 28, 2014
1 parent 8e08998 commit 3b058a8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
18 changes: 8 additions & 10 deletions Off the Record/OTRManagedAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,39 +209,37 @@ + (instancetype)createWithDictionary:(NSDictionary *)dictionary forContext:(NSMa

+(OTRManagedAccount *)accountForAccountType:(OTRAccountType)accountType
{
//Facebook
OTRManagedAccount * newAccount;
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
OTRManagedAccount * newAccount = nil;
if(accountType == OTRAccountTypeFacebook)
{
OTRManagedFacebookAccount * facebookAccount = [OTRManagedFacebookAccount MR_createEntity];
//Facebook
OTRManagedFacebookAccount * facebookAccount = [OTRManagedFacebookAccount MR_createInContext:localContext];
[facebookAccount setDefaultsWithDomain:kOTRFacebookDomain];
newAccount = facebookAccount;
}
else if(accountType == OTRAccountTypeGoogleTalk)
{
//Google Chat
OTRManagedGoogleAccount * googleAccount = [OTRManagedGoogleAccount MR_createEntity];
OTRManagedGoogleAccount * googleAccount = [OTRManagedGoogleAccount MR_createInContext:localContext];
[googleAccount setDefaultsWithDomain:kOTRGoogleTalkDomain];
newAccount = googleAccount;
}
else if(accountType == OTRAccountTypeJabber)
{
//Jabber
OTRManagedXMPPAccount * jabberAccount = [OTRManagedXMPPAccount MR_createEntity];
OTRManagedXMPPAccount * jabberAccount = [OTRManagedXMPPAccount MR_createInContext:localContext];
[jabberAccount setDefaultsWithDomain:@""];
newAccount = jabberAccount;
}
else if(accountType == OTRAccountTypeAIM)
{
//Aim
OTRManagedOscarAccount * aimAccount = [OTRManagedOscarAccount MR_createEntity];
OTRManagedOscarAccount * aimAccount = [OTRManagedOscarAccount MR_createInContext:localContext];
[aimAccount setDefaultsWithProtocol:kOTRProtocolTypeAIM];
newAccount = aimAccount;
}
if(newAccount)
{
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
}
[localContext MR_saveToPersistentStoreAndWait];
return newAccount;
}

Expand Down
5 changes: 5 additions & 0 deletions Off the Record/OTRManagedBuddy.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ +(instancetype)fetchOrCreateWithName:(NSString *)name account:(OTRManagedAccount
buddy = [self fetchWithName:name account:account inContext:context];
if (!buddy) {
buddy = [self MR_createInContext:context];
NSError *error = nil;
[context obtainPermanentIDsForObjects:@[buddy] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for buddy: %@", error);
}
buddy.accountName = name;
buddy.account = contextAccount;
}
Expand Down
8 changes: 5 additions & 3 deletions Off the Record/OTRManagedEncryptionStatusMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ @implementation OTRManagedEncryptionStatusMessage
+(OTRManagedEncryptionStatusMessage *)newEncryptionStatus:(OTRKitMessageState)newEncryptionStatus buddy:(OTRManagedBuddy *)buddy inContext:(NSManagedObjectContext *)context
{
OTRManagedEncryptionStatusMessage * encryptionStatusMessage = [OTRManagedEncryptionStatusMessage MR_createInContext:context];
NSError *error = nil;
[context obtainPermanentIDsForObjects:@[encryptionStatusMessage] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for OTRManagedEncryptionStatusMessage: %@", error);
}

encryptionStatusMessage.date = [NSDate date];
encryptionStatusMessage.isEncryptedValue = NO;
Expand All @@ -38,9 +43,6 @@ +(OTRManagedEncryptionStatusMessage *)newEncryptionStatus:(OTRKitMessageState)ne
break;
}




encryptionStatusMessage.message = message;
encryptionStatusMessage.statusValue = newEncryptionStatus;
encryptionStatusMessage.buddy = buddy;
Expand Down
8 changes: 6 additions & 2 deletions Off the Record/OTRManagedGroup.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "OTRManagedGroup.h"

#import "OTRLog.h"

@interface OTRManagedGroup ()

Expand All @@ -19,7 +19,11 @@ +(OTRManagedGroup *)fetchOrCreateWithName:(NSString *)name inContext:(NSManagedO

if (!group) {
group = [OTRManagedGroup MR_createInContext:context];

NSError *error = nil;
[context obtainPermanentIDsForObjects:@[group] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for OTRManagedGroup: %@", error);
}
group.name = name;
}
return group;
Expand Down
6 changes: 5 additions & 1 deletion Off the Record/OTRManagedMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ +(OTRManagedMessage*)newMessageWithBuddy:(OTRManagedBuddy *)theBuddy message:(NS
{
OTRManagedBuddy *localBuddy = [theBuddy MR_inContext:context];
OTRManagedMessage *managedMessage = [OTRManagedMessage MR_createInContext:context];

NSError *error = nil;
[context obtainPermanentIDsForObjects:@[managedMessage] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for message: %@", error);
}
managedMessage.uniqueID = [OTRUtilities uniqueString];
managedMessage.buddy = localBuddy;
managedMessage.messagebuddy = localBuddy;
Expand Down
7 changes: 6 additions & 1 deletion Off the Record/OTRManagedStatus.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "OTRManagedStatus.h"
#import "Strings.h"

#import "OTRLog.h"

@interface OTRManagedStatus ()

Expand Down Expand Up @@ -29,6 +29,11 @@ -(void)updateStatus:(OTRBuddyStatus)newStatus withMessage:(NSString *)newMessage
+(OTRManagedStatus *)newStatus:(OTRBuddyStatus)newStatus withMessage:(NSString *)newMessage withBuddy:(OTRManagedBuddy *)newBuddy incoming:(BOOL)newIsIncoming inContext:(NSManagedObjectContext *)context
{
OTRManagedStatus * managedStatus = [OTRManagedStatus MR_createInContext:context];
NSError *error = nil;
[context obtainPermanentIDsForObjects:@[managedStatus] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for managedStatus: %@", error);
}

managedStatus.statusValue = newStatus;

Expand Down
6 changes: 6 additions & 0 deletions Off the Record/OTRXMPPManagedPresenceSubscriptionRequest.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "OTRXMPPManagedPresenceSubscriptionRequest.h"

#import "OTRManagedXMPPAccount.h"
#import "OTRLog.h"

@interface OTRXMPPManagedPresenceSubscriptionRequest ()

Expand All @@ -25,6 +26,11 @@ +(OTRXMPPManagedPresenceSubscriptionRequest *)fetchOrCreateWith:(NSString *)jid
}
else{
OTRXMPPManagedPresenceSubscriptionRequest * newRequest = [OTRXMPPManagedPresenceSubscriptionRequest MR_createInContext:context];
NSError *error = nil;
[context obtainPermanentIDsForObjects:@[newRequest] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for subscription request: %@", error);
}
newRequest.jid = jid;
newRequest.xmppAccount = localAccount;
return newRequest;
Expand Down
30 changes: 23 additions & 7 deletions Off the Record/OTRvCard.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "OTRvCardAvatar.h"

#import "NSData+XMPP.h"
#import "OTRLog.h"


@interface OTRvCard ()
Expand All @@ -26,23 +27,33 @@ +(OTRvCard *)fetchOrCreateWithJidString:(NSString *)jidString inContext:(NSManag
}
else {
vCard = [OTRvCard MR_createInContext:context];
NSError *error = nil;
[context obtainPermanentIDsForObjects:@[vCard] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for vCard: %@", error);
}
vCard.jidString = jidString;
}
return vCard;
}

-(void)setVCardTemp:(XMPPvCardTemp *)vCardTemp {
if (!vCardTemp && self.vCardAvatarRelationship) {
[self.vCardTempRelationship MR_deleteEntity];
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
OTRvCard *localSelf = [self MR_inContext:localContext];
if (!vCardTemp && localSelf.vCardAvatarRelationship) {
[localSelf.vCardTempRelationship MR_deleteInContext:localContext];
}
else {

OTRvCardTemp * newvCardTemp = [OTRvCardTemp MR_createEntity];
OTRvCardTemp * newvCardTemp = [OTRvCardTemp MR_createInContext:localContext];
NSError *error = nil;
[localContext obtainPermanentIDsForObjects:@[newvCardTemp] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for OTRvCardTemp: %@", error);
}
newvCardTemp.vCardTemp = vCardTemp;
self.vCardTempRelationship = newvCardTemp;
localSelf.vCardTempRelationship = newvCardTemp;
}
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];

[localContext MR_saveToPersistentStoreAndWait];
}

-(XMPPvCardTemp *)vCardTemp {
Expand All @@ -59,6 +70,11 @@ -(void)setPhotoData:(NSData *)photoData
}
else {
OTRvCardAvatar * vCardAvatar = [OTRvCardAvatar MR_createInContext:context];
NSError *error = nil;
[context obtainPermanentIDsForObjects:@[vCardAvatar] error:&error];
if (error) {
DDLogError(@"Error obtaining permanent ID for vCardAvatar: %@", error);
}
vCardAvatar.photoData = photoData;
self.vCardAvatarRelationship = vCardAvatar;

Expand Down

0 comments on commit 3b058a8

Please sign in to comment.