I use Parse to save data and every time I add new data to server,TableView doesn't refresh and get new data
Is any way to refresh TableView except pull refresh ?
First of all Add Observer method which will notify when you.
NotificationCenter.default.post(name: Notification.Name("ReloadTableData"), object: nil)
Register that observer method in your view controller on which tableview you used.
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("ReloadTableData"), object: nil)
and create one function and reload your tableview
This might by your possible solutions for reloading without pull to refresh.
You can use this:
self.tableView.reloadData()
Whenever the user get back to the app or opens the app.
You can add in in the method
applicationWillEnterForeground
If the user is still in the app while the updates are being performed, then use push notification. In the method
appDidReceiveRemoteNotification
reload the data. You have to register for remote notification and all the process before you receive any push notification, but that is another issue.
first of all Adding refresh control to the tableview. So add the below code in ViewDidLoad.
let refresh = UIRefreshControl()
refresh.tintColor = UIColor.redColor()
refresh.attributedTitle = NSAttributedString(string:"Loading..!!", attributes: [NSForegroundColorAttributeName: UIColor.redColor()])
refresh.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresh)
self.tableView.sendSubviewToBack(refresh)
Now Calling the method when you Pull-To-refresh Tableview.
func handleRefresh(refreshControl : UIRefreshControl){
self.tableView.reloadData()
refreshControl.endRefreshing()
}
Refresh Control :
Implement a push notification or use LiveQuery on parse-server.
LiveQuery allows you to subscribe to a Parse.Query you are interested in. Once subscribed, the server will notify clients whenever a Parse.Object that matches the Parse.Query is created or updated, in real-time.
LiveQuery is already support IOS.
for more detail see the hyperLink.
https://github.com/ParsePlatform/parse-server
https://github.com/ParsePlatform/parse-server/wiki/Parse-LiveQuery
https://github.com/ParsePlatform/ParseLiveQuery-iOS-OSX
Related
I would like to perform one action when the user presses either volume button, and another when they stop pressing it, similar to what I can do by overriding touchesBegan() and touchesEnded.
I'm aware I can list to the volume level on change like so:
NotificationCenter.default.addObserver(self, selector: #selector(volumeChanged), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
#objc func volumeChanged(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let volumeChangeType = userInfo["AVSystemController_AudioVolumeChangeReasonNotificationParameter"] as? String {
// do something here, such as a switch based off of "volumeChangeType"
}
}
}
However, once the user has turned the volume up or down all the way, events are no longer fired. Also, no event is fired when the user stops pressing the button. This makes sense, because I'm actually listening to a volume change event, not a volume button press event.
Is there a way to listen to physical button presses in iOS?
Take a look at this GitHub Repo which looks like it provides everything that you're asking for.
In my TableViewController, I have an #objc function that fetches data using Alamofire. The function is called by the refreshController when the user pulls down to trigger a pull refresh.
The pull refresh is declared like so:
let refreshControl = UIRefreshControl()
tableView.refreshControl = refreshControl
self.refreshControl!.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)
And the function it runs:
#objc func refresh(sender:AnyObject) {
let defaults = UserDefaults.standard
if let mode = defaults.string(forKey: defaultsKeys.mode) {
self.mode = mode
}
self.tableData.removeAll()
print(mode)
Alamofire.request(mode).response { response in
let xml = SWXMLHash.config {
config in
config.shouldProcessLazily = true
}.parse(response.data!)
for elem in xml["rss"]["channel"]["item"].all {
self.tableData.append(elem["title"].element!.text)
}
print("Completed Data Gather")
self.mainTable?.reloadData()
self.refreshControl!.endRefreshing()
}
}
The issue comes when I attempt to call the pull refresh function programmatically using
self.refreshControl!.beginRefreshing()
self.refreshControl!.sendActions(for: .valueChanged)
The function executes (as observed in the console due to the print statements) and the data is fetched, but the tableView refuses to refresh. I know the data is fetched properly because the next time the user manually pull refreshes the tableView updates with the new data.
TL;DR My tableView properly reloads when the user manually pull refreshes, but not when the pull refresh is called programmatically using.
As Paul mentioned in the comments, this is because UI updates must be dispatched on the main thread.
Alamofire is asynchronous and runs on a background thread. With that being said, all of the code within your Alamofire request will execute on the background thread.
In order to execute code on the main thread, utilize the following:
DispatchQueue.main.async {
// Update UI ie. self.mainTable?.reloadData()
}
I’m using Twilio’s Programmable Voice in one of the projects. My primary requirement is to place VoIP class between mobile devices. I am able to place calls from one device to another,but when i accept the call at that time calling screen is dismiss automatically and call continue in background. In this case user do not have an option for disconnect call or any other action related to call because screen is dismissed.
Here is the screen that i have created for call when app is in foreground.
Calling placed success fully but on receiver accept it will dismiss the custom screen.So that user do not have any option to disconnect call or any other action related to call.
If any issue in code or any thing related to call kit setting i need to configure or any other issue ? Please help.
As per my knowledge this is default behaviour of call kit framework. On accept button click it will dismiss the screen when app is in foreground. If you wants to achieve same like whats app then you need to create a custom screen for that.Below code I did to resolve this issue:
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction)
{
NSLog("provider:performAnswerCallAction:")
// TwilioVoice.configureAudioSession()
let vc = loadVC(strStoryboardId: SB_CHAT, strVCId: idVoiceCallVC) as! VoiceCallVC
vc.callername = name
vc.userPhoto = userphoto
APP_DELEGATE.appNavigation?.pushViewController(vc, animated: true)
assert(action.callUUID == self.callInvite?.uuid)
TwilioVoice.isAudioEnabled = false
self.performAnswerVoiceCall(uuid: action.callUUID)
{ (success) in
if (success)
{
action.fulfill()
}
else
{
action.fail()
}
}
action.fulfill()
}
You just need to add your custom screen display code in this delegate method of call kit framework.
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {}
Thanks.
When the app is open or in background, I am using notification in appdelegate to update the message box table and then it is working.
Appdelegate (in didFinishLaunchingWithOptions and didReceiveRemoteNotification):
NSNotificationCenter.defaultCenter().postNotificationName("changeMessageBox", object: nil)
MessageViewController:
in viewdidload:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector (MessagesViewController.reloadtable), name: "changeMessageBox", object: nil)
func reloadtable()
{
clearArrays()
if (PFUser.currentUser()!["firebaseUID"] !== nil)
{
self.updateResultArray(PFUser.currentUser()!["firebaseUID"] as! String)
resultsTable.reloadData()
}
}
and data is refreshed in the message box.
But when the app is closed and i recieve a chat, the message box ( the window with messages from all the people not the individual chat) does not get updated.
I have to open the app, go to message box ,then back to main page and go back to message box again only then the table view gets refreshed.
Do you guys know what am i missing?
Try below code for post notification,
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("changeMessageBox", object: nil)
})
I was having same issue and resolved like this. Hope this will help you.
Receiving Notification while app in closed or in suspended state will trigger
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
Try to post notification in this method. i guess it will update your view.
Additionally Refer this document if you have query with Notifiacation
http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/
I have an IOS/Iwatch app which uses notifications to broadcast the reception of a message coming from the watch. My problem is that my notifications are sometimes sent (NSNotificationCenter) during the creation of a view that needs to receive it.
Typically it happens after the viewDidLoad() and before the viewDidAppear() hooks. I set the Observer in the first hook :
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "sessionDidEnd", name: MRNotifications.CurrentSessionDidEnd.rawValue, object: session.objectID)
}
But the view seem to never see the notification.
Starting from which state in its lifecycle can a View receive a notification ?