Skip to content

Commit

Permalink
Fix launch crash bug after first launch.
Browse files Browse the repository at this point in the history
Former-commit-id: 9ab2333
  • Loading branch information
leichunfeng committed Jul 4, 2015
1 parent 10cfab5 commit 2be690e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MVVMReactiveCocoa/MVVMReactiveCocoa-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>245</string>
<string>246</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down
2 changes: 2 additions & 0 deletions MVVMReactiveCocoa/Model/OCTUser+MRCPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef NS_ENUM(NSUInteger, OCTUserFollowingStatus) {
@property (assign, nonatomic) OCTUserFollowerStatus followerStatus;
@property (assign, nonatomic) OCTUserFollowingStatus followingStatus;

- (BOOL)mrc_updateRawLogin;

+ (BOOL)mrc_saveOrUpdateUsers:(NSArray *)users;
+ (BOOL)mrc_saveOrUpdateFollowerStatusWithUsers:(NSArray *)users;
+ (BOOL)mrc_saveOrUpdateFollowingStatusWithUsers:(NSArray *)users;
Expand Down
40 changes: 36 additions & 4 deletions MVVMReactiveCocoa/Model/OCTUser+MRCPersistence.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#import "OCTUser+MRCPersistence.h"

#define INSERT_STATEMENT @"INSERT INTO User VALUES (:id, :rawLogin, :login, :name, :bio, :email, :avatar_url, :html_url, :blog, :company, :location, :collaborators, :public_repos, :owned_private_repos, :public_gists, :private_gists, :followers, :following, :disk_usage);"
#define UPDATE_STATEMENT @"UPDATE User SET rawLogin = :rawLogin, login = :login, name = :name, bio = :bio, email = :email, avatar_url = :avatar_url, html_url = :html_url, blog = :blog, company = :company, location = :location, collaborators = :collaborators, public_repos = :public_repos, owned_private_repos = :owned_private_repos, public_gists = :public_gists, private_gists = :private_gists, followers = :followers, following = :following, disk_usage = :disk_usage WHERE id = :id;"
#define UPDATE_STATEMENT_LIST @"UPDATE User SET rawLogin = :rawLogin, login = :login, bio = :bio, avatar_url = :avatar_url, html_url = :html_url, collaborators = :collaborators, owned_private_repos = :owned_private_repos, public_gists = :public_gists, private_gists = :private_gists, disk_usage = :disk_usage WHERE id = :id;"
#define INSERT_STATEMENT @"INSERT INTO User (id, login, name, bio, email, avatar_url, html_url, blog, company, location, collaborators, public_repos, owned_private_repos, public_gists, private_gists, followers, following, disk_usage) VALUES (:id, :login, :name, :bio, :email, :avatar_url, :html_url, :blog, :company, :location, :collaborators, :public_repos, :owned_private_repos, :public_gists, :private_gists, :followers, :following, :disk_usage);"
#define UPDATE_STATEMENT @"UPDATE User SET login = :login, name = :name, bio = :bio, email = :email, avatar_url = :avatar_url, html_url = :html_url, blog = :blog, company = :company, location = :location, collaborators = :collaborators, public_repos = :public_repos, owned_private_repos = :owned_private_repos, public_gists = :public_gists, private_gists = :private_gists, followers = :followers, following = :following, disk_usage = :disk_usage WHERE id = :id;"
#define UPDATE_STATEMENT_LIST @"UPDATE User SET login = :login, bio = :bio, avatar_url = :avatar_url, html_url = :html_url, collaborators = :collaborators, owned_private_repos = :owned_private_repos, public_gists = :public_gists, private_gists = :private_gists, disk_usage = :disk_usage WHERE id = :id;"

@implementation OCTUser (MRCPersistence)

Expand Down Expand Up @@ -69,6 +69,35 @@ - (BOOL)mrc_delete {

#pragma mark - Save Or Update Users

- (BOOL)mrc_updateRawLogin {
__block BOOL result = YES;

[[FMDatabaseQueue sharedInstance] inDatabase:^(FMDatabase *db) {
FMResultSet *rs = [db executeQuery:@"SELECT * FROM User WHERE id = ? LIMIT 1;", self.objectID];

@onExit {
[rs close];
};

if (rs == nil) {
mrcLogLastError(db);
result = NO;
return;
}

if ([rs next]) {
BOOL success = [db executeUpdate:@"UPDATE User SET rawLogin = ? WHERE id = ?;", self.rawLogin, self.objectID];
if (!success) {
mrcLogLastError(db);
result = NO;
return;
}
}
}];

return result;
}

+ (BOOL)mrc_saveOrUpdateUsers:(NSArray *)users {
if (users.count == 0) return YES;

Expand Down Expand Up @@ -221,6 +250,9 @@ + (instancetype)mrc_currentUser {
OCTUser *currentUser = [[MRCMemoryCache sharedInstance] objectForKey:@"currentUser"];
if (!currentUser) {
currentUser = [self mrc_fetchUserWithRawLogin:[SSKeychain rawLogin]];

NSAssert(currentUser != nil, @"The retrieved currentUser must not be nil.");

[[MRCMemoryCache sharedInstance] setObject:currentUser forKey:@"currentUser"];
}
return currentUser;
Expand All @@ -232,7 +264,7 @@ + (instancetype)mrc_fetchUserWithRawLogin:(NSString *)rawLogin {
__block OCTUser *user = nil;

[[FMDatabaseQueue sharedInstance] inDatabase:^(FMDatabase *db) {
FMResultSet *rs = [db executeQuery:@"SELECT * FROM User WHERE login = ? OR email = ? LIMIT 1;", rawLogin, rawLogin];
FMResultSet *rs = [db executeQuery:@"SELECT * FROM User WHERE rawLogin = ? OR login = ? OR email = ? LIMIT 1;", rawLogin, rawLogin, rawLogin];

@onExit {
[rs close];
Expand Down
2 changes: 2 additions & 0 deletions MVVMReactiveCocoa/ViewModel/MRCLoginViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ - (void)initialize {
[[MRCMemoryCache sharedInstance] setObject:authenticatedClient.user forKey:@"currentUser"];

self.services.client = authenticatedClient;

[authenticatedClient.user mrc_saveOrUpdate];
[authenticatedClient.user mrc_updateRawLogin]; // The only place to update rawLogin, I hate the logic of rawLogin.

SSKeychain.rawLogin = authenticatedClient.user.rawLogin;
SSKeychain.password = self.password;
Expand Down

0 comments on commit 2be690e

Please sign in to comment.