Changeset 4004


Ignore:
Timestamp:
01/21/13 16:02:09 (18 months ago)
Author:
koke
Message:

On sync, change only posts/pages that are synced

We should never overwrite a post with server content it it's either
Pushing or Failed, or else we can lose the user's data

fixes #1545

Location:
trunk/WordPress/Classes
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/Classes/Blog.m

    r3995 r4004  
    848848    NSMutableArray *postsToKeep = [NSMutableArray array]; 
    849849    for (NSDictionary *postInfo in newPosts) { 
    850         Post *newPost = [Post createOrReplaceFromDictionary:postInfo forBlog:self]; 
    851         if (newPost != nil) { 
    852             [postsToKeep addObject:newPost]; 
    853         } else { 
    854             WPFLog(@"-[Post createOrReplaceFromDictionary:forBlog:] returned a nil post: %@", postInfo); 
    855         } 
     850        NSNumber *postID = [[postInfo objectForKey:@"postid"] numericValue]; 
     851        Post *newPost = [Post findOrCreateWithBlog:self andPostID:postID]; 
     852        if (newPost.remoteStatus == AbstractPostRemoteStatusSync) { 
     853            [newPost updateFromDictionary:postInfo]; 
     854        } 
     855        [postsToKeep addObject:newPost]; 
    856856    } 
    857857 
     
    898898    NSMutableArray *pagesToKeep = [NSMutableArray array]; 
    899899    for (NSDictionary *pageInfo in newPages) { 
    900         Page *newPage = [Page createOrReplaceFromDictionary:pageInfo forBlog:self]; 
    901         if (newPage != nil) { 
    902             [pagesToKeep addObject:newPage]; 
    903         } else { 
    904             WPFLog(@"-[Page createOrReplaceFromDictionary:forBlog:] returned a nil page: %@", pageInfo); 
    905         } 
     900        NSNumber *pageID = [[pageInfo objectForKey:@"postid"] numericValue]; 
     901        Page *newPage = [Page findOrCreateWithBlog:self andPageID:pageID]; 
     902        if (newPage.remoteStatus == AbstractPostRemoteStatusSync) { 
     903            [newPage updateFromDictionary:pageInfo]; 
     904        } 
     905        [pagesToKeep addObject:newPage]; 
    906906    } 
    907907 
  • trunk/WordPress/Classes/Page.h

    r3995 r4004  
    1616 
    1717#pragma mark Class Methods 
    18 // Creates an empty local post associated with blog 
     18/** 
     19 Creates an empty local post associated with blog 
     20 */ 
    1921+ (Page *)newDraftForBlog:(Blog *)blog; 
    20 + (Page *)findWithBlog:(Blog *)blog andPostID:(NSNumber *)postID; 
    21 // Takes the NSDictionary from a XMLRPC call and creates or updates a post 
    22 + (Page *)createOrReplaceFromDictionary:(NSDictionary *)postInfo forBlog:(Blog *)blog; 
     22 
     23/** 
     24 Retrieves the page with the specified `pageID` for a given blog 
     25 
     26 @returns the specified page. Returns nil if there is no page with that id on the blog 
     27 */ 
     28+ (Page *)findWithBlog:(Blog *)blog andPageID:(NSNumber *)pageID; 
     29 
     30/** 
     31 Retrieves the page with the specified `pageID` for a given blog. If the specified page doesn't exist, a new empty one is created 
     32 
     33 @returns the specified page. 
     34 */ 
     35+ (Page *)findOrCreateWithBlog:(Blog *)blog andPageID:(NSNumber *)pageID; 
     36 
     37/** 
     38 Updates the page properties with the results of a XML-RPC call 
     39 
     40 @param pageInfo a dictionary with values returned from wp.getPages 
     41 */ 
     42- (void)updateFromDictionary:(NSDictionary *)pageInfo; 
    2343 
    2444@end 
  • trunk/WordPress/Classes/Page.m

    r3995 r4004  
    2222@end 
    2323 
    24 @interface Page (PrivateMethods) 
    25 - (void )updateFromDictionary:(NSDictionary *)postInfo; 
    26 @end 
    27  
    28  
    2924@implementation Page 
    3025@dynamic parentID; 
     
    5045} 
    5146 
    52 + (Page *)findWithBlog:(Blog *)blog andPostID:(NSNumber *)postID { 
    53     NSSet *results = [blog.posts filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"postID == %@",postID]]; 
     47+ (Page *)findWithBlog:(Blog *)blog andPageID:(NSNumber *)pageID { 
     48    NSSet *results = [blog.posts filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"postID == %@", pageID]]; 
    5449     
    5550    if (results && (results.count > 0)) { 
     
    5954} 
    6055 
    61 + (Page *)createOrReplaceFromDictionary:(NSDictionary *)postInfo forBlog:(Blog *)blog { 
    62     Page *page = [self findWithBlog:blog andPostID:[postInfo objectForKey:@"page_id"]]; 
     56+ (Page *)findOrCreateWithBlog:(Blog *)blog andPageID:(NSNumber *)pageID { 
     57    Page *page = [self findWithBlog:blog andPageID:pageID]; 
    6358     
    6459    if (page == nil) { 
    6560        page = [Page newPageForBlog:blog]; 
     61        page.postID = pageID; 
     62        page.remoteStatus = AbstractPostRemoteStatusSync; 
    6663    } 
    6764         
    68         [page updateFromDictionary:postInfo]; 
    6965    return page; 
    7066} 
  • trunk/WordPress/Classes/Post.h

    r3995 r4004  
    6767 
    6868/** 
    69  Takes the NSDictionary from a XMLRPC call and creates or updates a post 
     69 Retrieves the post with the specified `postID` for a given blog. If the specified post doesn't exist, a new empty one is created 
     70 
     71 @returns the specified post. 
    7072 */ 
    71 + (Post *)createOrReplaceFromDictionary:(NSDictionary *)postInfo forBlog:(Blog *)blog; 
     73+ (Post *)findOrCreateWithBlog:(Blog *)blog andPostID:(NSNumber *)postID; 
     74 
     75/** 
     76 Updates the post properties with the results of a XML-RPC call 
     77 
     78 @param postInfo a dictionary with values returned from wp.getPosts 
     79 */ 
     80- (void)updateFromDictionary:(NSDictionary *)postInfo; 
    7281 
    7382///------------------------ 
  • trunk/WordPress/Classes/Post.m

    r3995 r4004  
    3838@interface Post(PrivateMethods) 
    3939+ (Post *)newPostForBlog:(Blog *)blog; 
    40 - (void)updateFromDictionary:(NSDictionary *)postInfo; 
    4140- (void)uploadInBackground; 
    4241- (void)didUploadInBackground; 
     
    8079} 
    8180 
    82 + (Post *)createOrReplaceFromDictionary:(NSDictionary *)postInfo forBlog:(Blog *)blog { 
    83     Post *post = [self findWithBlog:blog andPostID:[[postInfo objectForKey:@"postid"] numericValue]]; 
     81+ (Post *)findOrCreateWithBlog:(Blog *)blog andPostID:(NSNumber *)postID { 
     82    Post *post = [self findWithBlog:blog andPostID:postID]; 
    8483     
    8584    if (post == nil) { 
    8685        post = [Post newPostForBlog:blog]; 
    87     } 
    88         [post updateFromDictionary:postInfo]; 
     86        post.postID = postID; 
     87        post.remoteStatus = AbstractPostRemoteStatusSync; 
     88    } 
    8989    [post findComments]; 
    9090    return post; 
    9191} 
    92  
    93  
    94  
    9592 
    9693- (id)init { 
Note: See TracChangeset for help on using the changeset viewer.