同步优化, 冲突判断以内容为准

This commit is contained in:
life
2015-11-11 14:56:06 +08:00
parent f08316799f
commit 940a4b69a8
8 changed files with 105 additions and 22 deletions

View File

@@ -247,7 +247,7 @@
}
//
if(![Common isNull:files]) {
if(![Common isNullOrNil:files]) {
int i = 0;
for(File *file in files) {
NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{
@@ -323,7 +323,7 @@
}
//
if(![Common isNull:files]) {
if(![Common isNullOrNil:files]) {
int i = 0;
for(File *file in files) {
NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{

View File

@@ -23,7 +23,8 @@
#pragma 同步操作
- (BOOL)deleteNoteForceByServerNoteId:(NSString *)serverNoteId;
- (Note *)getNoteByServerNoteId:(NSString *)serverNoteId;
- (Note *)updateNoteForce:(id)obj;
- (Note *)updateNoteForce:(id)obj
content:(NSString*) content;
- (Note *)addNoteForce:(id)obj;
- (void)copyNoteForConflict:(id)obj localNote:(Note *)localNote;
@@ -56,6 +57,10 @@
- (void) getNoteContent:(Note *) note
success:(void (^)(NSString *))success
fail:(void (^)())fail;
- (void) getNoteContent:(NSString *) serverNoteId
isMarkdown:(BOOL) isMarkdown
success:(void (^)(NSString *))success
fail:(void (^)())fail;
+ (void) getImage:(NSString *)serverFileId
success:(void (^)(NSString *))success

View File

@@ -219,7 +219,9 @@
//
// save
- (Note *)updateNoteForce:(id)obj {
// , , fixed
- (Note *)updateNoteForce:(id)obj
content:(NSString*) content {
NSString *serverNoteId = obj[@"NoteId"];
Note *note = [self getNoteByServerNoteId:serverNoteId];
@@ -249,13 +251,21 @@
note.usn = usn;
note.isTrash = isTrash;
//
if([Common isNullOrNil:content]) {
note.isInitSync = M_YES; //
}
else {
note.isInitSync = M_NO;
note.content = content;
}
note.isDirty = M_NO;
note.localIsNew = M_NO;
note.updatedTime = [Common goDate:obj[@"UpdatedTime"]];
NSString *tagsStr;
if(![Common isNull:tags]) {
if(![Common isNullOrNil:tags]) {
tagsStr = [tags componentsJoinedByString:@","];
}
else {
@@ -275,7 +285,7 @@
// recountTag, tag, tag, tag, tag count
[TagService recountTagNoteCountByTitles:tags inContext:self.tmpContext];
if(![Common isNull:everTags]) {
if(![Common isNullOrNil:everTags]) {
[TagService recountTagNoteCountByTitles:[everTags componentsSeparatedByString:@","] inContext:self.tmpContext];
}
return note;
@@ -319,7 +329,7 @@
note.userId = [UserService getCurUserId];
NSString *tagsStr;
if(![Common isNull:tags]) {
if(![Common isNullOrNil:tags]) {
tagsStr = [tags componentsJoinedByString:@","];
}
else {
@@ -750,6 +760,22 @@
}
}
- (void) getNoteContent:(NSString *) serverNoteId
isMarkdown:(BOOL) isMarkdown
success:(void (^)(NSString *))success
fail:(void (^)())fail
{
[ApiService getNoteContent:serverNoteId success:^(id obj) {
NSString *content = [self fixContent:obj[@"Content"] isMarkdown:isMarkdown];
success(content);
} fail:^{
if(fail) {
fail();
}
}];
}
#pragma
@@ -877,7 +903,7 @@
}
// files
NSArray *files = ret[@"Files"];
if(![Common isNull:files] && [files count] > 0) {
if(![Common isNullOrNil:files] && [files count] > 0) {
for(NSDictionary *file in files) {
NSString *localFileId = file[@"LocalFileId"];
NSString *serverFileId = file[@"FileId"];
@@ -924,15 +950,44 @@
// ,
if(msg && [msg isEqualToString:@"conflict"]) {
[ApiService getNote:note.serverNoteId success:^(id obj) {
// [self copyNoteForConflict:obj localNote:note];
// sync
// , , ,
[self getNoteContent:note.serverNoteId isMarkdown:[note.isMarkdown boolValue] success:^(NSString * content) {
// , 使
if ([note.content isEqualToString:content]) {
[self updateNoteForce:obj content:content];
if(success) {
success();
}
}
// ,
else {
[self copyNoteForConflict:obj localNote:note];
} fail:^{
}];
}
}
if(fail) {
//
fail(ret);
}
}
} fail:^{
// ,
[self copyNoteForConflict:obj localNote:note];
if(fail) {
//
fail(ret);
}
}];
} fail:^{
if(fail) {
//
fail(ret);
}
}];
}
}
}];
}

View File

@@ -162,12 +162,28 @@ BOOL inSyncing = NO;
// , ,
if ([note.isDirty boolValue]) {
NSLog(@"有冲突 服务器USN=%ld 本地note:%@", (long)[usn integerValue], note);
// , , ,
[_noteService getNoteContent:note.serverNoteId isMarkdown:[note.isMarkdown boolValue] success:^(NSString * content) {
// , 使
if ([note.content isEqualToString:content]) {
[_noteService updateNoteForce:eachObj content:content];
}
// ,
else {
[_noteService copyNoteForConflict:eachObj localNote:note];
}
} fail:^{
// ,
[_noteService copyNoteForConflict:eachObj localNote:note];
}];
}
// ,
else {
NSLog(@" updateNoteForce %@", note);
[_noteService updateNoteForce:eachObj];
[_noteService updateNoteForce:eachObj content:nil];
}
}
}

View File

@@ -139,7 +139,7 @@
+ (void)recountTagNoteCountByTitlesStr:(NSString *)titlesStr
inContext:(NSManagedObjectContext *)inContext
{
if(![Common isNull:titlesStr]) {
if(![Common isNullOrNil:titlesStr]) {
NSArray *titles = [titlesStr componentsSeparatedByString:@","];
for(NSString *title in titles) {
[self.class recountTagNoteCount:title inContext:inContext];
@@ -153,7 +153,7 @@
+ (void)recountTagNoteCountByTitles:(NSArray *)titles
inContext:(NSManagedObjectContext *)inContext
{
if(![Common isNull:titles]) {
if(![Common isNullOrNil:titles]) {
for(NSString *title in titles) {
[self.class recountTagNoteCount:title inContext:inContext];
}

View File

@@ -9,7 +9,8 @@
#import <Foundation/Foundation.h>
@interface Common : NSObject
+ (BOOL) isNull:(id) i;
//+ (BOOL) isNull:(id) i;
+ (BOOL) isNullOrNil:(id) i;
+ (BOOL) isBlankString:(NSString *)string;
+ (NSString *) getDocPath;
+ (NSString *) getAbsPath:(NSString *)relatedPath;

View File

@@ -14,6 +14,7 @@
@implementation Common
/*
+ (BOOL) isNull:(id) i {
if(i) {
return (NSNull *)i == [NSNull null];
@@ -23,6 +24,11 @@
}
// return !i || (NSNull *)i == [NSNull null];
}
*/
+ (BOOL) isNullOrNil:(id) i {
return !i || (NSNull *)i == [NSNull null];
}
+ (BOOL) isBlankString:(NSString *)string {
if (string == nil || string == NULL) {

View File

@@ -142,7 +142,7 @@
return nil;
}
LeaImageViewController *vc = self.views[index];
if(vc && ![Common isNull:vc]) {
if(vc && ![Common isNullOrNil:vc]) {
return self.views[index];
}
@@ -151,9 +151,9 @@
NSString *fileId = [Common getFileIdFromUrl:url];
UIImage *img;
if(![Common isNull:fileId]) {
if(![Common isNullOrNil:fileId]) {
NSString *absPath = [FileService getFileAbsPathByFileIdOrServerFileId:fileId];
if (![Common isNull:absPath]) {
if (![Common isNullOrNil:absPath]) {
img = [[UIImage alloc] initWithContentsOfFile:absPath];
}
}