Parse saveInBackgroundWithBlock additional task

115 views Asked by At

What I want to do is cause another save anytime saveInBackgroundWithBlock is called in my app. Im doing this to create a "log" of everything that changes and who changed it. I looked into the PFObject.h but I am not sure how to properly write this in Obj-C or where else/how I could do this other than going through and adding it to every single instance. Is this a good way to do it? Or should I stick it out and use the harder way?

- (void)saveInBackgroundWithBlock:(nullable PFBooleanResultBlock)block;
1

There are 1 answers

0
Ran Hassid On BEST ANSWER

you have multiple ways in order to achieve this kind of functionality:

  1. Create Util class which receive PFObject and PFBooleanResultBlock as parameters this Util class will execute a call to saveInBackgroundWithBlock inside the callback you can implement the additional save that you need. at the end your Util class should look like the following:

@interface ParseUtils : NSObject

+ (void)saveParseObject:(PFObject *)object block:(PFBooleanResultBlock)block;

@end

@implementation ParseUtils

+ (void)saveParseObject:(PFObject *)object block:(PFBooleanResultBlock)block {
    
    // if the object is nil or it was not changed return
    if (!object || !object.isDirty) return;
    
    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        
        // if no error occured
        if (!error){
            
            // HERE YOU SHOULD CALL YOUR ADDITIONAL SAVE...
            
        }
        
        // handle the callback to the calling class
        if (block){
            block(succeeded,error);
        }
        
    }];
    
}

@end

  1. Another option is to do the same but with singleton (if you created some REST Client singleton for your project)

  2. Another nice option is to create category on top of the PFObject and there to create a method which do exactly the same like the method that of the util

There are more options but i think that it's enough.. if you want to do it as quick as possible use the utilities. if you want to do it with best architecture then go with the category.