How to implement a counter with CloudKit? - cloudkit

Situation
I want to manage a counter (the number of times of button is pressed) with CloudKit.
Each time a user press a button, I want to increment an integer. This integer will hence be a global (for all users) counter of how many times the button has been pressed.
Question
Is there a preferred way to do it?
My idea
I could store a CKRecord with this number and fetch and modify it each time the button is pressed. But, isn't it unsafe since it might be updated by many people (more or less) at the same time?

Your idea would work. When doing an update you will first fetch the counter record, increment the counter and then save it back. If another user has updated the counter in the mean time, then CloudKit will give you an error and you can try it again.
But it does depend on how often users will press the button. If you have multiple pushes per second during a longer period, then you will probably get a lot of errors when updating the data. The time before an update succeeds will grow.

Related

How to set remainder countdown by getting values from database in flutter

In this screen I am creating ride, and taking all these information include (Date and Time of ride) from the driver. After creating a ride I am saving all the data into firestore and getting data into next screen. I am also pasting the screenshot of next screen below
I want to show the countdown according to the date & time and when the countdown end, the start button will enable. how can we do it ? Kindly help me
First of all you have to get difference of time. Start countdown timer with that time. Once you get zero you can enable start button based on that condition. You can see this solution.

Swift storing an Integer and displaying the changed Integer when reopening the app

I have an app where when I click a button it increases the value of an Int. When I close the app and then open it again, I want the value of the Int to be whatever it was before closing it. This is in Swiftui and I have multiple Int values that I want to store and reload after closing and opening the app. I dont want where the user clicks a save button to save the Int value, but rather it saves automatically.
I tried AppStorage, but this slowed down the app way too much.
If it's not too much data you can use UserDefaults.

How to make a button trigger an event (Timer)?

I am working on a fire evacuation project and would like to trigger an event after clicking a button. Basically I want to begin the fire evacuation process manually using the button feature but would like to trigger a timer for about 60 seconds to give the pedestrians time to evacuate using the event feature.
I have tried to make pedestrians stop the evacuation process by manually clicking another button but would like to use an event trigger timer instead as this will be a better way to conduct my simulation. I cannot seem to trigger an event once the button is clicked. I've tried changing the trigger type but not sure where to go from there. Is there any code that is required or another step that is required?
Even though a dynamic event is fine in your application, you can also use a user control event:
And then in your button you can use the following code:
event.restart(60,SECOND); //you can choose any timeout here
or
event.restart(); // this will use the configuration, which in this case I set up to 60 seconds
The difference between a dynamic event and a user controlled event is that if you click the button again:
With the dynamic event, you will generate another instance of the event, meaning that if you click the button at t=0 seconds, and t=30 seconds, you will generate 2 events at t=60 seconds and t=90 seconds
With the user control event, you will restart the same event. if you click the button at t=0 seconds, and t=30 seconds, you will generate a unique event at t=90 seconds.
So depending on which one you prefer... you can choose. I would prefer the user control in case you click the button 2 times... or if you regret clicking it so fast... I don't know
Use the Dynamic Event for this.
Set it up by pulling it from the AnyLogic Agent Library to the Workspace and give it a name (here: MyDynamicEvent).
Add the code you want to have executed when the timer runs off in the Action field
Set one (or several) timed instance for this event by using the code create_MyDynamicEvent(60,SECOND);. Note: The syntax of this statement is always create_ followed by the actual name you gave your DynamicEvent type.

Wicket DefaultDataTable - Refresh it on browser back button

In my application I am using DefaultDataTable with SortableDataProvider which has LoadableDetachableModel as the model.
I used it to display a set of records ( say RecordList page). When I add or remove some records and load the page RecordList again, it displays the changes. However, if I use the back button of the browser and go the RecordList page which was rentered earlier ( before adding/ removing the records ). The DefaultDataTable still has the old sets of records. This is a big issue when records are deleted.
For example, If I delete a record and press back button then the page fails as the record it is trying to show does not exist in the database. Adding does not create an issue because it simply does not get listed in the set of records.
In another page, I just have PageableListView with LoadableDetachableModel. It works fine with out an issue.
There are one or two things that you can do about this.
First please check whether the data provider has the latest data.
Secondly make sure that the set of records with the Data provider is refreshed when the browser back button is hit. Ensure that the data fed to the Data Provider is refreshed in a method other than constructor. Because constructors will not be called when you hit back. So you would need to use a separate method. Ideally you could use the same method, but called from both constructor and when back button is pressed.

Reason for 5 sec delay to show an activity on pressing the home button?

I am facing delay of 5 sec to show an Activity after pressing the HOME key. I had gone through some post and found that after pressing the HOME button, android prevents services and broadcast-receivers from calling startActivity for 5 seconds.
Q1. Can anyone please let me know the reason to happening this delay ?
Q2. When I modified the ActivityManagerService::checkAppSwitchAllowedLocked() to always return true, it avoids checking lock and hence Activity gets shown without any delay. Would it be a good solution to avoid delay or it may cause any wrong effect in other ways ?
I know, it's probably too late (half year passed). However, here is the answer
You won't be able to get this permission on not rooted phone, unless your application is signed with the same keys as system. Android has some permissions like that.
Also, you may be interested to check this question. Starting an activity from a service after HOME button pressed without the 5 seconds delay