I am using Swift as a programming language and Firebase for my backend. My use case is about creating an event (a tournament) which has different rounds (RoundOf16, Quarter-Finale, Semi-Finale etc.)
Whenever a new rounds starts and finishes, I want to send out notifications to the participants. It would be easy to do that by using local notifications.
However, additionally to sending the notifications, I also want to trigger a function to check if a participant can make it to the next round (if a round is finished) or if the event has even enough participants to start.
How can I do that? Is there a client-side way to achieve this or can it only be done server-side? The functions would need to run completely in the background as it is the case that you close the app, even though the round is still active.
We have to consider that multiple tournaments are going to be created. This means, for each tournament, we have couple of rounds with a beginning and ending date.
What would be the most elegant way? Having one function where I pass the tournamentId so based on that, I can check who won the round? But wouldn't I overwrite the function to be triggered every time then?
Related
The Bloc manual describes the example of a simple Todos app. It works as an example, but I get stuck when trying to make it into a more realistic app. Clearly, a more realistic Todos app needs to keep working when the user temporarily loses network connection, and also needs to occasionally check the server for updates that the user might have added from another device.
So as a basic data model I have:
dataFromServer, which is refreshed every five minutes, and
localData, that describes what changes have been made locally but haven't been synchronized to the server yet.
My current idea is to have three kinds of events:
on<GetTodosFromServer>() which runs every few minutes to check the server for updates and only changes the dataFromServer,
on<TodoAdded>() (and its friends TodoDeleted, TodoChecked, and so on) which get triggered when the user changes the data, and only change the localData, and
on<SyncTodoToServer>() which runs whenever the user changes the todo list, or when network connectivity is restored, and tries to send the changes to the server, retrieves the new value from the server, and then sets the new dataFromServer and localData.
So obviously there's a lot of interaction between these three methods. When a new todo is added after the synchronization to the server starts, but before synchronization is finished, it needs to stay in the local changes object. When GetTodosFromServer and SyncTodoToServer both return server data, they need to find out who has the latest data and keep that. And so on.
Coming from a Redux background, I'm used to having two reducers (one for local data, one for server data) that would only respond to simple actions. E.g. an action { "type": "TodoSuccessfullySyncedToServer", uploadedData: [...], serverResponse: [...] } would be straightforward to parse for both the localData and the dataFromServer reducer. The reducer doesn't contain any of the business logic, it receives actions one by one and all you need to think about inside the reducer is the state before the action, the action itself, and the state after the action. Anything you rely on to handle the action will be in the action itself, not in the context. So different pieces of code that generate those actions can just fire these actions without thinking, knowing that the reducer will handle them correctly.
Bloc on the other hand seems to mix business logic and updating the state. API calls are made within the event handlers, which will emit a value possibly many seconds later. So every time you return from an asynchronous call in an event handler, you need to think about how the state might have changed while that call was happening and the consequences this has on what you're currently doing. Also, an object in the state can be updated by different events that need to coordinate among themselves how to avoid conflicts while doing so.
Is there a best practice on how to avoid the complexity that brings? Is it best practice to split large events into "StartSyncToServer" and "SuccessfullySyncedToServer" events where the second behaves a lot like a Redux reducer? I don't see any of that in the examples, so is there another way this complexity is typically avoided in Bloc? Or is Bloc entirely unopinionated on these things?
I'm not looking for personal opinions here, only if there's something I missed in the Bloc manual (or other authoritative source) about how this was intended to work.
I am working on radio application where i need to convert speech to text. For that i am using third party api's. For geting better results i want to run two api's at the same time and compare the output. this should happen when user clicks on record button.
I know we can do this using GCD but not getting exact idea of how we can achieve this.
Need suggestion.
Thank you.
Th short answer is that you create two GCD queues, one for each Speech-to-Text task. Within each block, you call the two different APIs with the same input data. Then you either wait for the result, or get the block to invoke a callback status method when completed.
Note that you will need to ensure that the speech engines can safely run on background threads.
This is fairly straightforward if you want to record the audio first, then submit the data to two different engines for processing. But it sounds like you might want to start processing the audio as soon as the user clicks Record? In that case, it very much depends on the APIs as to how you feed them data in real time. You might want to just run them on separate threads explicitly and feed them data as it comes in.
I'm currently working on a new game for iOS using Cocos2D. The game needs to advance states after x amount of time since the first launch. So for example:
State - Time
initial launch
24hrs
48hrs
My first idea was to just get the data and time on first launch and save it to a file. Then I could check it ever now and again to see how much time has passed. The problem with this is I need it to be in realtime so that the changes will take effect immediately once the state is reached. It also needs to continue when the user is not using the app. The functionality I'm looking for is kind of similar to how the iOS strategy games work where you build structures that take x amount of time.
Anyway my question(s) is; is there some sort of library that can accomplish this and how can I get it to continue after the user exits the app?
It can't. There is - apart from kind of misusing video/music playing etc. no way for your app to do work while it is not running.
You have two things you can do to simulate that behavior (and I suppose the strategy games do this, too):
You can calculate at any time while a user is still running your app the points in the future when something should happen (eg a housing structure is finished). When the user leave your app, store these future times as local events - then the user will get notified that something has happened in your game (eg message "The church has been built. Do you want to go to church now?)". Pressing yes will open your app, and you can do whatever is necessary to indeed build the church. So in fact you don't do it at the time when it occurred, but when the user opens your app the next time.
Like 1, but without notification. Just remember when the user leaves the app (eg in your settings, I would use a property list; set it when the app delegate gets the appWillResignActive event), and the next time he starts do whatever would have been done in the meantime - he won't be able to tell the difference :-).
It's all about make believe here :-).
Is there any way that two (or more) UILocalNotifications which fires at the exact same time can be merged together. Let's say that I have two reminders that fires at 12.00PM today:
1) Wash the dishes
2) Buy milk
What I have right now is (since I have scheduled two separate timers) two single alerts coming up; one telling me to wash the dishes and the other to buy milk.
What I try to achieve is one alert, telling me both to wash the dishes and to buy milk.
I have read that for tasks like this one, APNS might be a better choice, but due to lack of a proper and stable server, and to keep the complexity as low as possible, I am researching to find out whether this could be done just using UILocalNotifications.
The only solution I have come up with is to create some logic that checks if there's any notifications with the same fire dates and if there is, removing them both and creating a new, merged notification with information from both.
Any suggestions?
The only solution I have come up with is to create some logic that checks if there's any notifications with the same fire dates and if there is, removing them both and creating a new, merged notification with information from both.
That's exactly the way to go. It should not be very difficult to implement, either.
you can use singletons to achieve this
http://blog.mugunthkumar.com/coding/iphone-tutorial-scheduling-local-notifications-using-a-singleton-class/
iPhone Development:
I want to have an infinite loop constantly checking an NSMutableArray and if [size > 0] pull the first object, do something with it, remove it, wait .25 seconds, then continue checking.
I want my GUI buttons to add this list. Is there a way to do this? Is there a way to do this by having the loop in the main thread?
UPDATE
I guess i didn't ask the question properly. I'm using the AsyncSocket class to send telnet commands. I need to send them fast because their used to control a RC car, however the RC receiver is a tad slow. So i want to slow downs the rate in which my iPhone sends out the commands. Right now i have button events sending out the commands but i want to instead have the events add the command to a list/queue/array. In parralel to the button clicks I want a process to check the queue every .25 seconds and send out the commands in the queue.
MY PROBLEM:
the AsyncSocket isn't thread safe so I cannot have another thread sending commands. The documentation reads
If you find yourself on a thread which
is different from the thread on which
AsyncSocket is running, and you need
to invoke a method on AsyncSocket,
then you must use a method like
performSelector:onThread: to ensure
the method is invoked on AsyncSocket's
thread (and thereby maintaining
thread-safety)."
I dont know what this means...
What does this quote above mean?
Is multithreading required for what i want to do?
I would recommend looking into Key Value Observing. This will allow you to set up an observing class to be notified when entries are made into the array.
It'll be a bit harder than the standard tutorials you'll find, most simply observe a property being updated. You'll need to use -willChange:valuesAtIndexes:forKey: and -didChange:valuesAtIndexes:forKey: when you add elements to the array. If all you need to listen for is items added, this should be enough. However, you can look into ObservingOptions to really get finer control.
See more about NSKeyValueObserving here: http://developer.apple.com/library/ios/#documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html#//apple_ref/occ/cat/NSKeyValueObserving
This method will allow you to not waste any cycles polling for changes.
This is easily solved by using an NSOperationsQueue.
add an NSTimer with interval 0.25 that will be cheking it in a loop.
Check documentation. There are some useful sample-projects