Play framework, fake formdata for batch call - playframework-2.2

I'm trying to implement a batch call method for inserting a lot of objects at once.
The thing is, the method for individual inserts gets a Form for the insert, which is bound from the Request.
My idea here is to retrieve a json array, with all the data for the required inserts.
I would then loop over it and call the regular insert method for each one. But that means having to pass the data to the Form. But the Form gets data from seeing if there's data attached to the HTTP Request. But there isn't an HTTP Request.
Is there a way for me to do this without using a FakeRequest?

Related

Fetch data after an update

VueJS + Quasar + Pinia + Axios
Single page application
I have an entity called user with 4 endpoints associated:
GET /users
POST /user
PUT /user/{id}
DELETE /user/{id}
When I load my page I call the GET and I save the response slice of users inside a store (userStore)
Post and Put returns the created/updated user in the body of the response
Is it a good practice to manually update the slice of users in the store after calling one of these endpoints, or is better to call the GET immediatly after ?
If you own the API or can be sure about the behavior of what PUT/POST methods return, you can use local state manipulation. Those endpoints should return the same value as what the GET endpoint returns inside. Otherwise, you might end up with incomplete or wrong data on the local state.
By mutating the state locally without making an extra GET request, the user can immediately see the change in the browser. It will also be kinder to your server and the user's data usage.
However, if creating the resource(user, in this case) was a really common operation accessible by lots of users, then calling the GET endpoint to return a slice would be better since it would have more chance to include the new ones that are created by other users. But, in that case, listening to real-time events(i.e. using WebSockets) would be even better to ensure everyone gets accurate and new data in real-time.

Update single attribute with Restkit

I want to update ONE SINGLE attribute on the Core Data Object and send the change to the server using RestKit.
I see that ResKit is always sending Object with ALL attributes and not only with the changed ones. That makes my app slow(er).
I also see that update response from server should return whole object back to the RestKit which is again slower that it could be (All I need is success/failure response)
Is there elegant solution for this? (I am pretty new to the RestKit)
You can create a request descriptor on-the-fly which includes only the attributes you want to send, you would either simply need to use a mapping operation yourself or be sure to only run one such operation at a time and call removeRequestDescriptor: on the object manager (this could be tricky for you to manage). The alternate is to put the data to upload into a dictionary and upload that, but that isn't ideal.
For the response, the mapping says what to map but it doesn't all have to be there, RestKit will take whatever it can and map that.

AFNetworking get Parameters from Operation

I am attempting to get the parameters for a POST request sent via the AFNetworking pod. However, I can't seem to get them. I am looping through the active operations with the below:
for(AFHTTPRequestOperation* operation in manager.operationQueue.operations){
NSLog(#"%#",operation.request.URL.path);
}
However, I can't get the parameters. I've tried using operation.request.URL.parameterString, but since it is POST, the string is null. Anyone know how to get these? I'd like to collect them so that I can cancel requests that are specific to the path and parameters sent, ensuring I'll get down to just the single request I need to cancel.
I ended up going about this a different way. I created a class that handled all the AFNetworking path calls. In this class is a dictionary which stored an integer id for the call and the operation. As the operations complete or fail, they are removed. The integer id is passed back to the calling object, allowing for unique access for canceling requests or polling.

mvc4 passing data between paging action and views

i have an action which will invoke a service (not database)to get some data for display,and i want to do paging on these data.however,every time a second page is clicked,it will invoke this action and of course invoke the service again,actually when i click the first page link,it already generate the whole data including what the second page needs. i just want to invoke the service once and get all the data,and later when paging,i don't need to invoke the service again,how can i deal with that?hope someone could give me a hint~
There are several ways to address this. If it's practical and a limited amount of data, it's ok to return the entire data set in the first request.
If that's your case I would consider returning a pure JSON object when you load the page initially. You can then deserialize this into a JS object variable on the web page that you can perform your paging operations against. This is an example of client side paging where all the data exists client side.
Another approach is to do Ajax based paging where you request the data for the next page as needed. I would still recommend returning JSON in this scenario as well.
The two approaches differ in that the first one returns all the data upfront, whereas the second one only returns what you need to render any given page.

Two Asynchronous NSURLRequests using Cocoa Touch

I am using the example shown here - listing's 1-5 to request the content of a URL and everything is working perfectly. Now my problem is, while the first request is sent off, I want to request content from a second web page, my problem is, even if I duplicate everything there for a second request and connection, and create another variable for NSMutableData that should hold the second request's data, I am only getting the second requests data filled into *receivedData (i.e. the original first requests NSMutableData)
Its like the moment I add code for a second request, only the second request is executing...
Does anyone know how to modify the code in the linked example so I can issue two separate requests?
Disclaimer: learning objective-C now, my background is C#/VB.
If you are using "self" as the delegate for both requests, then you'll be getting callbacks for both of the requests and you are most likely only writing the bytes into a single receivedData object. What you need to do is either create a new class to act as the delegate and create a new instance of the class for each request, or if you want to continue using "self" as the delegate make sure you take a look at the "connection" parameter passed into the delegate methods. That will indicate which connection the callback belongs to, allowing you to write the data to the appropriate receivedData object.