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;
|
int i = 0;
|
||||||
for(File *file in files) {
|
for(File *file in files) {
|
||||||
NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{
|
NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{
|
||||||
@@ -323,7 +323,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 处理文件
|
// 处理文件
|
||||||
if(![Common isNull:files]) {
|
if(![Common isNullOrNil:files]) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(File *file in files) {
|
for(File *file in files) {
|
||||||
NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{
|
NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{
|
||||||
|
@@ -23,7 +23,8 @@
|
|||||||
#pragma 同步操作
|
#pragma 同步操作
|
||||||
- (BOOL)deleteNoteForceByServerNoteId:(NSString *)serverNoteId;
|
- (BOOL)deleteNoteForceByServerNoteId:(NSString *)serverNoteId;
|
||||||
- (Note *)getNoteByServerNoteId:(NSString *)serverNoteId;
|
- (Note *)getNoteByServerNoteId:(NSString *)serverNoteId;
|
||||||
- (Note *)updateNoteForce:(id)obj;
|
- (Note *)updateNoteForce:(id)obj
|
||||||
|
content:(NSString*) content;
|
||||||
- (Note *)addNoteForce:(id)obj;
|
- (Note *)addNoteForce:(id)obj;
|
||||||
- (void)copyNoteForConflict:(id)obj localNote:(Note *)localNote;
|
- (void)copyNoteForConflict:(id)obj localNote:(Note *)localNote;
|
||||||
|
|
||||||
@@ -56,6 +57,10 @@
|
|||||||
- (void) getNoteContent:(Note *) note
|
- (void) getNoteContent:(Note *) note
|
||||||
success:(void (^)(NSString *))success
|
success:(void (^)(NSString *))success
|
||||||
fail:(void (^)())fail;
|
fail:(void (^)())fail;
|
||||||
|
- (void) getNoteContent:(NSString *) serverNoteId
|
||||||
|
isMarkdown:(BOOL) isMarkdown
|
||||||
|
success:(void (^)(NSString *))success
|
||||||
|
fail:(void (^)())fail;
|
||||||
|
|
||||||
+ (void) getImage:(NSString *)serverFileId
|
+ (void) getImage:(NSString *)serverFileId
|
||||||
success:(void (^)(NSString *))success
|
success:(void (^)(NSString *))success
|
||||||
|
@@ -219,7 +219,9 @@
|
|||||||
|
|
||||||
// 更新笔记本
|
// 更新笔记本
|
||||||
// 调用者save
|
// 调用者save
|
||||||
- (Note *)updateNoteForce:(id)obj {
|
// 如果传了内容, 表示是有内容的, 这里的内容已经fixed
|
||||||
|
- (Note *)updateNoteForce:(id)obj
|
||||||
|
content:(NSString*) content {
|
||||||
NSString *serverNoteId = obj[@"NoteId"];
|
NSString *serverNoteId = obj[@"NoteId"];
|
||||||
Note *note = [self getNoteByServerNoteId:serverNoteId];
|
Note *note = [self getNoteByServerNoteId:serverNoteId];
|
||||||
|
|
||||||
@@ -249,13 +251,21 @@
|
|||||||
note.usn = usn;
|
note.usn = usn;
|
||||||
note.isTrash = isTrash;
|
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.isDirty = M_NO;
|
||||||
note.localIsNew = M_NO;
|
note.localIsNew = M_NO;
|
||||||
note.updatedTime = [Common goDate:obj[@"UpdatedTime"]];
|
note.updatedTime = [Common goDate:obj[@"UpdatedTime"]];
|
||||||
|
|
||||||
NSString *tagsStr;
|
NSString *tagsStr;
|
||||||
if(![Common isNull:tags]) {
|
if(![Common isNullOrNil:tags]) {
|
||||||
tagsStr = [tags componentsJoinedByString:@","];
|
tagsStr = [tags componentsJoinedByString:@","];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -275,7 +285,7 @@
|
|||||||
|
|
||||||
// recountTag, 之前的tag, 现在的tag, 得到不一样的tag, 只分析不同的tag count
|
// recountTag, 之前的tag, 现在的tag, 得到不一样的tag, 只分析不同的tag count
|
||||||
[TagService recountTagNoteCountByTitles:tags inContext:self.tmpContext];
|
[TagService recountTagNoteCountByTitles:tags inContext:self.tmpContext];
|
||||||
if(![Common isNull:everTags]) {
|
if(![Common isNullOrNil:everTags]) {
|
||||||
[TagService recountTagNoteCountByTitles:[everTags componentsSeparatedByString:@","] inContext:self.tmpContext];
|
[TagService recountTagNoteCountByTitles:[everTags componentsSeparatedByString:@","] inContext:self.tmpContext];
|
||||||
}
|
}
|
||||||
return note;
|
return note;
|
||||||
@@ -319,7 +329,7 @@
|
|||||||
note.userId = [UserService getCurUserId];
|
note.userId = [UserService getCurUserId];
|
||||||
|
|
||||||
NSString *tagsStr;
|
NSString *tagsStr;
|
||||||
if(![Common isNull:tags]) {
|
if(![Common isNullOrNil:tags]) {
|
||||||
tagsStr = [tags componentsJoinedByString:@","];
|
tagsStr = [tags componentsJoinedByString:@","];
|
||||||
}
|
}
|
||||||
else {
|
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 发送改变
|
#pragma 发送改变
|
||||||
|
|
||||||
|
|
||||||
@@ -877,7 +903,7 @@
|
|||||||
}
|
}
|
||||||
// files
|
// files
|
||||||
NSArray *files = ret[@"Files"];
|
NSArray *files = ret[@"Files"];
|
||||||
if(![Common isNull:files] && [files count] > 0) {
|
if(![Common isNullOrNil:files] && [files count] > 0) {
|
||||||
for(NSDictionary *file in files) {
|
for(NSDictionary *file in files) {
|
||||||
NSString *localFileId = file[@"LocalFileId"];
|
NSString *localFileId = file[@"LocalFileId"];
|
||||||
NSString *serverFileId = file[@"FileId"];
|
NSString *serverFileId = file[@"FileId"];
|
||||||
@@ -924,15 +950,44 @@
|
|||||||
// 冲突, 复制之
|
// 冲突, 复制之
|
||||||
if(msg && [msg isEqualToString:@"conflict"]) {
|
if(msg && [msg isEqualToString:@"conflict"]) {
|
||||||
[ApiService getNote:note.serverNoteId success:^(id obj) {
|
[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:^{
|
} fail:^{
|
||||||
|
if(fail) {
|
||||||
|
// 是否冲突
|
||||||
|
fail(ret);
|
||||||
|
}
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(fail) {
|
|
||||||
// 是否冲突
|
|
||||||
fail(ret);
|
|
||||||
}
|
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -162,12 +162,28 @@ BOOL inSyncing = NO;
|
|||||||
// 本地修改了, 有冲突, 则复制一个
|
// 本地修改了, 有冲突, 则复制一个
|
||||||
if ([note.isDirty boolValue]) {
|
if ([note.isDirty boolValue]) {
|
||||||
NSLog(@"有冲突 服务器USN=%ld 本地note:%@", (long)[usn integerValue], note);
|
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 {
|
else {
|
||||||
NSLog(@" updateNoteForce %@", note);
|
NSLog(@" updateNoteForce %@", note);
|
||||||
[_noteService updateNoteForce:eachObj];
|
[_noteService updateNoteForce:eachObj content:nil];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -139,7 +139,7 @@
|
|||||||
+ (void)recountTagNoteCountByTitlesStr:(NSString *)titlesStr
|
+ (void)recountTagNoteCountByTitlesStr:(NSString *)titlesStr
|
||||||
inContext:(NSManagedObjectContext *)inContext
|
inContext:(NSManagedObjectContext *)inContext
|
||||||
{
|
{
|
||||||
if(![Common isNull:titlesStr]) {
|
if(![Common isNullOrNil:titlesStr]) {
|
||||||
NSArray *titles = [titlesStr componentsSeparatedByString:@","];
|
NSArray *titles = [titlesStr componentsSeparatedByString:@","];
|
||||||
for(NSString *title in titles) {
|
for(NSString *title in titles) {
|
||||||
[self.class recountTagNoteCount:title inContext:inContext];
|
[self.class recountTagNoteCount:title inContext:inContext];
|
||||||
@@ -153,7 +153,7 @@
|
|||||||
+ (void)recountTagNoteCountByTitles:(NSArray *)titles
|
+ (void)recountTagNoteCountByTitles:(NSArray *)titles
|
||||||
inContext:(NSManagedObjectContext *)inContext
|
inContext:(NSManagedObjectContext *)inContext
|
||||||
{
|
{
|
||||||
if(![Common isNull:titles]) {
|
if(![Common isNullOrNil:titles]) {
|
||||||
for(NSString *title in titles) {
|
for(NSString *title in titles) {
|
||||||
[self.class recountTagNoteCount:title inContext:inContext];
|
[self.class recountTagNoteCount:title inContext:inContext];
|
||||||
}
|
}
|
||||||
|
@@ -9,7 +9,8 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
@interface Common : NSObject
|
@interface Common : NSObject
|
||||||
+ (BOOL) isNull:(id) i;
|
//+ (BOOL) isNull:(id) i;
|
||||||
|
+ (BOOL) isNullOrNil:(id) i;
|
||||||
+ (BOOL) isBlankString:(NSString *)string;
|
+ (BOOL) isBlankString:(NSString *)string;
|
||||||
+ (NSString *) getDocPath;
|
+ (NSString *) getDocPath;
|
||||||
+ (NSString *) getAbsPath:(NSString *)relatedPath;
|
+ (NSString *) getAbsPath:(NSString *)relatedPath;
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
@implementation Common
|
@implementation Common
|
||||||
|
|
||||||
|
/*
|
||||||
+ (BOOL) isNull:(id) i {
|
+ (BOOL) isNull:(id) i {
|
||||||
if(i) {
|
if(i) {
|
||||||
return (NSNull *)i == [NSNull null];
|
return (NSNull *)i == [NSNull null];
|
||||||
@@ -23,6 +24,11 @@
|
|||||||
}
|
}
|
||||||
// return !i || (NSNull *)i == [NSNull null];
|
// return !i || (NSNull *)i == [NSNull null];
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
+ (BOOL) isNullOrNil:(id) i {
|
||||||
|
return !i || (NSNull *)i == [NSNull null];
|
||||||
|
}
|
||||||
|
|
||||||
+ (BOOL) isBlankString:(NSString *)string {
|
+ (BOOL) isBlankString:(NSString *)string {
|
||||||
if (string == nil || string == NULL) {
|
if (string == nil || string == NULL) {
|
||||||
|
@@ -142,7 +142,7 @@
|
|||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
LeaImageViewController *vc = self.views[index];
|
LeaImageViewController *vc = self.views[index];
|
||||||
if(vc && ![Common isNull:vc]) {
|
if(vc && ![Common isNullOrNil:vc]) {
|
||||||
return self.views[index];
|
return self.views[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,9 +151,9 @@
|
|||||||
NSString *fileId = [Common getFileIdFromUrl:url];
|
NSString *fileId = [Common getFileIdFromUrl:url];
|
||||||
UIImage *img;
|
UIImage *img;
|
||||||
|
|
||||||
if(![Common isNull:fileId]) {
|
if(![Common isNullOrNil:fileId]) {
|
||||||
NSString *absPath = [FileService getFileAbsPathByFileIdOrServerFileId:fileId];
|
NSString *absPath = [FileService getFileAbsPathByFileIdOrServerFileId:fileId];
|
||||||
if (![Common isNull:absPath]) {
|
if (![Common isNullOrNil:absPath]) {
|
||||||
img = [[UIImage alloc] initWithContentsOfFile:absPath];
|
img = [[UIImage alloc] initWithContentsOfFile:absPath];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user