GithubHelp home page GithubHelp logo

Comments (7)

kastiglione avatar kastiglione commented on September 28, 2024

Let's see, here's a few thoughts:

  1. You don't need the top level +createSignal: in -saveTagListSignal:. You can simply return a signal composition based off -rac_findObjects.
  2. Instead of chaining with -doNext:, try writing this with -flattenMap: and compare the two approaches.
  3. Why are you using -deliverOn:?
  4. I noticed you're logging completion, have a look at -logCompleted, and friends -logAll, -logNext, and -logError.
  5. Instead of iterating over each tag name, and creating a single tag in -saveTagList: using -rac_save, you could batch the tag objects into an array and call +[PFObject rac_saveAll:].

Hope that helps, feel free to follow up with more questions.

from parse-racextensions.

bangedorrunt avatar bangedorrunt commented on September 28, 2024

Hi @kastiglione, your pointer is excelent. Here is my feedback:

  1. I did notice that and removed those lines
  2. -doNext: means only inject side effect after there is a subscription to signal, -flattenMap: convert existing signal into new signal of signals and flatten it into a single new signal, am I correct?
  3. -deliverOn: makes me confused. I thought I will use it whenever I want my tasks done in background just as same as I use with dispatchasync??? I assume you question mark it because -saveInBackground: already run my task in background, so there is no need to use -deliverOn:. Am I correct?
  4. I have no idea about those until now, I will learn them asap bcoz it would help me trace back my signal process???
  5. You're absolutely right on the money. I just read over your header files and realized Parse also has the corressponding methods.

Here is my new line of codes. Look pretty much cleaner now, please give me your view on it :)

- (RACSignal *)saveTagListSignal:(NSArray *)listOfTagArray {
    // Perform scanning tag database, if new tag is matched with query data, skip that tag
    PFQuery *tagQuery = [PFQuery queryWithClassName:@"Tag"];
    [tagQuery selectKeys:@[@"tagName"]];
    return [[tagQuery rac_findObjects] flattenMap:^ (NSArray *objects) {
        __block NSArray *pfTagObjects = objects;
        __block NSArray *filteredTagList;

        // Filter the existing tag list, only pick the tag that doesn't exist in the database.
        if ([pfTagObjects count] > 0) { // If there is tags existing in the database
            filteredTagList = _.array(listOfTagArray)
            .reject(^ BOOL (id tagArrayElement){
                return _.array(pfTagObjects).any(^ BOOL (id pfTagObject){
                    return [[pfTagObject objectForKey:@"tagName"] isEqualToString:tagArrayElement];
                });
            }).unwrap;

            // Save filtered tag list to the server first
            filteredTagList = _.array(filteredTagList).map(^(NSString *tag){
                return [self saveTagList:tag];
            }).unwrap;
            return [PFObject rac_saveAll:filteredTagList];
        } else { //  If there is no tag in database

            // Fix the case that duplicated tags are allowed to created if there is no tag in database
            filteredTagList = _.uniq(listOfTagArray);
            // Save new tag list to server
            filteredTagList = _.array(filteredTagList).map(^(NSString *tag){
                return [self saveTagList:tag];
            }).unwrap;
            return [PFObject rac_saveAll:filteredTagList];
        };
    }];
}
- (PFObject *)saveTagList:(NSString *)tag {
    PFObject *newTag = [PFObject objectWithClassName:@"Tag"];
    [newTag setObject:tag forKey:@"tagName"];
    [newTag setObject:[PFUser currentUser] forKey:@"creator"];
    return newTag;
}

from parse-racextensions.

kastiglione avatar kastiglione commented on September 28, 2024
  1. -doNext: means only inject side effect after there is a subscription to signal, -flattenMap: convert existing signal into new signal of signals and flatten it into a single new signal, am I correct?

Close, -doNext: is for side effects on each value sent, not once per subscription. And yes, that's correct about -flattenMap:.

  1. -deliverOn: makes me confused. I thought I will use it whenever I want my tasks done in background just as same as I use with dispatchasync??? I assume you question mark it because -saveInBackground: already run my task in background, so there is no need to use -deliverOn:. Am I correct?

Correct. Parse-RACExtensions uses Parse's InBackground methods, and so the work already happens off the main thread. You would want to use deliverOn:RACScheduler.mainThreadScheduler in the case where you were making UI changes.

  1. I have no idea about those until now, I will learn them asap bcoz it would help me trace back my signal process???

Yep, they log the even, the signal name, and potentially other information. They can be more helpful than adding manual log statements, and they're quick and easy to add to the code.

from parse-racextensions.

kastiglione avatar kastiglione commented on September 28, 2024

Further to number 2., if you find yourself using a -subscribe: inside of a signal chain/composition, you should consider whether you really want one of the signal of signals operators, like -flatten, -concat, or -switchToLatest.

from parse-racextensions.

kastiglione avatar kastiglione commented on September 28, 2024

As for the code, I think there's some redundant code that can be pulled out of the if/else block:

// Save new tag list to server
filteredTagList = _.array(filteredTagList).map(^(NSString *tag){
    return [self saveTagList:tag];
}).unwrap;
return [PFObject rac_saveAll:filteredTagList];

The if/else block could simply build the filteredTagList.

from parse-racextensions.

bangedorrunt avatar bangedorrunt commented on September 28, 2024

Thanks heaps @kastiglione 🌟 for helping me point out my problem. I'm so satisfied with your answer. I will close this issue now and another question is coming soon 👍

from parse-racextensions.

kastiglione avatar kastiglione commented on September 28, 2024

😎

from parse-racextensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.