Skip to content

Commit 2be690e

Browse files
committed
Fix launch crash bug after first launch.
Former-commit-id: 9ab2333
1 parent 10cfab5 commit 2be690e

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

MVVMReactiveCocoa/MVVMReactiveCocoa-Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</dict>
7373
</array>
7474
<key>CFBundleVersion</key>
75-
<string>245</string>
75+
<string>246</string>
7676
<key>Fabric</key>
7777
<dict>
7878
<key>APIKey</key>

MVVMReactiveCocoa/Model/OCTUser+MRCPersistence.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ typedef NS_ENUM(NSUInteger, OCTUserFollowingStatus) {
2626
@property (assign, nonatomic) OCTUserFollowerStatus followerStatus;
2727
@property (assign, nonatomic) OCTUserFollowingStatus followingStatus;
2828

29+
- (BOOL)mrc_updateRawLogin;
30+
2931
+ (BOOL)mrc_saveOrUpdateUsers:(NSArray *)users;
3032
+ (BOOL)mrc_saveOrUpdateFollowerStatusWithUsers:(NSArray *)users;
3133
+ (BOOL)mrc_saveOrUpdateFollowingStatusWithUsers:(NSArray *)users;

MVVMReactiveCocoa/Model/OCTUser+MRCPersistence.m

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#import "OCTUser+MRCPersistence.h"
1010

11-
#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);"
12-
#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;"
13-
#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;"
11+
#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);"
12+
#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;"
13+
#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;"
1414

1515
@implementation OCTUser (MRCPersistence)
1616

@@ -69,6 +69,35 @@ - (BOOL)mrc_delete {
6969

7070
#pragma mark - Save Or Update Users
7171

72+
- (BOOL)mrc_updateRawLogin {
73+
__block BOOL result = YES;
74+
75+
[[FMDatabaseQueue sharedInstance] inDatabase:^(FMDatabase *db) {
76+
FMResultSet *rs = [db executeQuery:@"SELECT * FROM User WHERE id = ? LIMIT 1;", self.objectID];
77+
78+
@onExit {
79+
[rs close];
80+
};
81+
82+
if (rs == nil) {
83+
mrcLogLastError(db);
84+
result = NO;
85+
return;
86+
}
87+
88+
if ([rs next]) {
89+
BOOL success = [db executeUpdate:@"UPDATE User SET rawLogin = ? WHERE id = ?;", self.rawLogin, self.objectID];
90+
if (!success) {
91+
mrcLogLastError(db);
92+
result = NO;
93+
return;
94+
}
95+
}
96+
}];
97+
98+
return result;
99+
}
100+
72101
+ (BOOL)mrc_saveOrUpdateUsers:(NSArray *)users {
73102
if (users.count == 0) return YES;
74103

@@ -221,6 +250,9 @@ + (instancetype)mrc_currentUser {
221250
OCTUser *currentUser = [[MRCMemoryCache sharedInstance] objectForKey:@"currentUser"];
222251
if (!currentUser) {
223252
currentUser = [self mrc_fetchUserWithRawLogin:[SSKeychain rawLogin]];
253+
254+
NSAssert(currentUser != nil, @"The retrieved currentUser must not be nil.");
255+
224256
[[MRCMemoryCache sharedInstance] setObject:currentUser forKey:@"currentUser"];
225257
}
226258
return currentUser;
@@ -232,7 +264,7 @@ + (instancetype)mrc_fetchUserWithRawLogin:(NSString *)rawLogin {
232264
__block OCTUser *user = nil;
233265

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

237269
@onExit {
238270
[rs close];

MVVMReactiveCocoa/ViewModel/MRCLoginViewModel.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ - (void)initialize {
3232
[[MRCMemoryCache sharedInstance] setObject:authenticatedClient.user forKey:@"currentUser"];
3333

3434
self.services.client = authenticatedClient;
35+
3536
[authenticatedClient.user mrc_saveOrUpdate];
37+
[authenticatedClient.user mrc_updateRawLogin]; // The only place to update rawLogin, I hate the logic of rawLogin.
3638

3739
SSKeychain.rawLogin = authenticatedClient.user.rawLogin;
3840
SSKeychain.password = self.password;

0 commit comments

Comments
 (0)