Why use testEntityManager.persist method instead of repository.save method? - jpa

When doing a save test in a repository test,
Why use testEntityManager.persist method instead of repository.save method?
testEntityManager.persist -> persist
repository.save -> persist in method
Eventually, both execute persist . What's the difference?

Related

StreamBuilder triggers a method twice while the same operation as a variable triggered only once

I have a StreamBuilder that accepts a stream from my service. It looks like this:
StreamBuilder(
stream: MyService.getStream$()
builder (...)
);
Plus, I have my service with the following method:
getStream$() {
print('being printed twice');
return Observable.just('text')
.doOnData(() => print('being printed twice too'));
}
When I run the app, I get the following prints being printed twice (each).
But, when I change the following implementation as a variable, it runs just once:
Observable getStream = Observable
.just('text')
.doOnData((data) => print('being printed once');
Of course, in the example above, I would use variables, but in my original code, I'm unable to do so because I'm depended on the instance properties.
What I can do, is to declare an Observable variable and in the constructor to set it to my desired observable. Although, this solution sounds like a workaround, and I'm not sure why would the the method would be triggered twice.
Any ideas?
StreamBuilder is rebuild every time build method is invoked. It means that MyService.getStream$() is evaluated with each invocation and creates new Observable. If you assign Observable to variable it will be created once and reused between build calls.
Take a look at this question Future running twice because it's in build method, how to fix it?
it's about future but the mechanism is same.

How to make a function can be called only by specific class in swift?

I have two frameworks A and B. I have a public function inside B called getMap() which returns a copy of a map(which is a private variable in B). So I call getMap() in A to get this value. This is fine because it's a copy so whatever I do to the returned value it doesn't affect the actual variable inside B.
Now I did some processing to this value, I need to pass it back to B. Here is the problem: In order to pass it back, it has to be a public function, but I don't want other frameworks or application to call this function because only A should be making changes to this map value.
Is there any way to specify in B that only if A is calling the function then the value should be set, otherwise ignoring anyone else who is using this function? I've heard you can use delegate/protocol to achieve this but I don't understand.
Yes you should be able to achieve this using delegate/protocols. The protocol will still have to be public which means any class could implement the protocol but the simple solution to this would be to just not implement it any other class.
You would create the the protocol in framework B like this:
protocol FrameworkBDelegate {
func sendMapChanges(map: Map)
}
Then call the delegate method when you're ready send it back to the other framework:
func changeMap() {
var map = frameworkA.getMap()
// Do stuff to map...
delegate.sendMapChanges(map: map)
}
I don't want to write a whole tutorial on how implement delegation so here's a good one from Swift by Sundell: here
Let me know if you need any help.
This is not possible out of the box.
If you create a Framework than you have to decide if a method/class needs to be public or not. If a method is public than there isn't an out of the box solution which delivers a bullet proof solution which solves your requirement. In the end the method is public to ALL consumers of Framework B.
So, you will end up implementing some kind of access control mechanisms within Framework B. This means that Framework A needs to authenticate itself in some way (access code etc.).
The delegate pattern will not solve your issue as well, hence it also has to be public, so that Framework A can use it. However, if it's public than all consumers of Framework B can use it.

JS/CoffeScript Call a method when an object is constructed

I have the following code
class MyCompo
constructor: (component) ->
#component = $(component)
#setEvents()
setEvents: ->
#component.on "click", #handleMyEvent
handleMyEvent: =>
...
that works just fine.
Now I need to fire handleMyEvent also when the object is constructed.
I have tried with DOMNodeInserted as follows
setEvents: ->
#component.on "DOMNodeInserted", #handleMyEvent
#component.on "click", #handleMyEvent
but I have some strange behavior (the method is called multiple times and not for all objects).
After further research, I found out that DOMNodeInserted even deprecated.
Is there an event that is fired when the object is constructed?
I know I could add it to the constructor itself but if possible I'd rather keeping everything on setEvents`.
Thanks

Spring annotation for Reactor.receive

I am working on Spring Reactor to write REST Services. I was wondering if there is an annotation to Reactor.receive method like we have #Selector and #ReplyTo
Such that :
in.consume(req -> reactor.sendAndReceive("test.httprequests", Event.wrap(req.getUri()), (Event<String>ev) -> {
invokes the annotated method.
You should be able to just return a value from the method you annotate with #Selector to handle test.httprequests events. The wiring bean should treat that method the same as a receive, which is actually just an alias for being aware of the replyTo.

In "cache" class, where does directory setup go?

I have a class that is used to store and retrieve image data from a cache using NSFileManager. When an instance of it is created, I want to check whether the image directory already exists, and if not, create it. Where is the most appropriate place to put this code? Is this something one would override the designated initializer for?
Thanks for reading.
The initialiser (init) function would be the best place to do it from a programming perspective because the rest of the instance methods would probably rely on having access to the directory to store/retrieve images.
You'd also want any instances created to know whether the accessing/creating was successful so in your initialiser you may wish to put some error handling which returns a nil instance (or throws an exception) if it can't be accessed which can then be handled by the classes which use the instance of your cache class.