Comments (7)
Let's see, here's a few thoughts:
- You don't need the top level
+createSignal:
in-saveTagListSignal:
. You can simply return a signal composition based off-rac_findObjects
. - Instead of chaining with
-doNext:
, try writing this with-flattenMap:
and compare the two approaches. - Why are you using
-deliverOn:
? - I noticed you're logging completion, have a look at
-logCompleted
, and friends-logAll
,-logNext
, and-logError
. - 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.
Hi @kastiglione, your pointer is excelent. Here is my feedback:
- I did notice that and removed those lines
-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?-deliverOn:
makes me confused. I thought I will use it whenever I want my tasks done in background just as same as I use withdispatchasync
??? 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?- I have no idea about those until now, I will learn them asap bcoz it would help me trace back my signal process???
- 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.
-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:
.
-deliverOn:
makes me confused. I thought I will use it whenever I want my tasks done in background just as same as I use withdispatchasync
??? 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.
- 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.
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.
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.
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.
😎
from parse-racextensions.
Related Issues (20)
- Best way to use `rac_countObjects`? HOT 14
- Thought about synchronous request! HOT 2
- Ideal API for #7 HOT 4
- Remove Parse SDK NSError* Normalization? HOT 3
- Update Parse dependency to 1.3.0 HOT 5
- rac_deleteAll hangs indefinitely HOT 8
- rac_findObjects doesn't react well to multiple subscribers HOT 5
- Parse Config Support HOT 2
- Update PodSpec HOT 2
- rac_refresh shouldn't be deprecated. HOT 5
- rac_findObjects just hangs with firstOrDefault HOT 3
- -[PFObject saveEventually] does not necessarily complete at a sensible time; misleading API HOT 2
- how does this work with the parse request count? HOT 1
- Write tests
- Write documentation
- OS X support
- Implement PFFile properly
- Implement Facebook and Twitter
- [pfobject rac_save] is not fired HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parse-racextensions.