mirror of
https://github.com/leanote/leanote-ios.git
synced 2025-10-15 15:40:44 +00:00
同步优化, 冲突判断以内容为准
This commit is contained in:
@@ -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:@{
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
|
||||
note.isInitSync = M_YES; // 需要重新同步笔记
|
||||
// 是否传了内容
|
||||
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];
|
||||
// [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];
|
||||
if(fail) {
|
||||
// 是否冲突
|
||||
fail(ret);
|
||||
}
|
||||
}
|
||||
} fail:^{
|
||||
// 内容得不到, 就当作是冲突来处理
|
||||
[self copyNoteForConflict:obj localNote:note];
|
||||
if(fail) {
|
||||
// 是否冲突
|
||||
fail(ret);
|
||||
}
|
||||
}];
|
||||
|
||||
} fail:^{
|
||||
if(fail) {
|
||||
// 是否冲突
|
||||
fail(ret);
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
if(fail) {
|
||||
// 是否冲突
|
||||
fail(ret);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
|
@@ -162,12 +162,28 @@ BOOL inSyncing = NO;
|
||||
// 本地修改了, 有冲突, 则复制一个
|
||||
if ([note.isDirty boolValue]) {
|
||||
NSLog(@"有冲突 服务器USN=%ld 本地note:%@", (long)[usn integerValue], note);
|
||||
[_noteService copyNoteForConflict:eachObj localNote: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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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];
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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) {
|
||||
|
@@ -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];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user