Swift Parse - local datastore and displaying objects in a tableview
It's always a good idea to avoid pointer lol ... so why not saving the userid or username with the specific object..
so change this line:
parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")
TO
parseLighthouse["username"] = PFUser.currentUser().username
Answer
NOW let's create a struct that contains the objectID and the Name outside of your Controller Class.
struct Data
{
var Name:String!
var id:String!
}
then inside of the Controller class, declare the following line of code globally
var ArrayToPopulateCells = [Data]()
Then your query function will look like :
func performQuery() {
let query = PFQuery(className: "ParseLighthouse")
query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
print(object.objectId)
print(object.objectForKey("Name"))
var singleData = Data()
singleData.id = object.objectId
singleData.Name = object["Name"] as! String
self.ArrayToPopulateCells.append(singleData)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
In the tableView numberOfRowinSection()
return ArrayToPopulateCells.count
In the cellForRowAtIndexPath()
var data = ArrayToPopulateCells[indexPath.row]
cell.textlabel.text = data.objectID
cell.detailLabel.text = data.Name
VOila that should be it
Swift Parse - local datastore items not appearing in tableView
I think your problem is you need to call
self.tableView.reloadData()
outside the for object in light {
loop
I think your data is being added to the array ok, its just the table view needs to know when its done.
EDIT***
func performQuery() {
let query = PFQuery(className: "ParseLighthouse")
query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
var singleData = localData()
singleData.name = object["Name"] as! String
singleData.note = object["Note"] as! String
singleData.date = object["Date"] as! String
singleData.latt = object["Latt"] as! NSNumber
singleData.longi = object["Longi"] as! NSNumber
singleData.lattDelta = object["LattDelta"] as! NSNumber
singleData.longiDelta = object["LongiDelta"] as! NSNumber
singleData.locality = object["Locality"] as! String
self.arrayToPopulateCells.append(singleData)
}
self.tableView.reloadData()
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
swift parse how to save objects in local datastore and show them?
Before calling findObjectsInBackgroundWithBlock
,
query.fromLocalDatastore()
This will force your query to pull from localDataStore. Your code should look like below:
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil && objects != nil
{
self.messages = objects!
for object in objects! {
inboxphotos.append(object.objectForKey("photo") as! PFFile)
inboxusers.append(object.objectForKey("username") as! String) }
}
}
In order to save objects locally, you must first enable the localDataStore:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.enableLocalDatastore()
Parse.setApplicationId("parseAppId", clientKey: "parseClientKey")
}
}
The recommended way for saving objects with localDataStore
is to use saveEventually
:
myObject.saveEventually()
You may also use .pinInBackground()
to keep objects in the localDataStore "permanently". Pinning objects makes sure that local objects are automatically updated when you fetch them again from the server. It is an ideal way to implement offline caching of server data.
Related Topics
iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture
Checkbox Uitableview with Different Sections
Swift Computed Properties Cannot Be Used in Init
Video with Gpuimagechromakeyfilter Has Tint When Played in Transparent Gpuimageview
What Is a Safe Way to Turn Streamed (Utf8) Data into a String
Reversing a Range Results in Mismatching Types
How to Draw a Simple Rounded Rect in Swift (Rounded Corners)
What Is the Purpose of Firebase Appcheck
Create Hash in Swift Using Key and Message
Saving Array Using Nsuserdefaults Crashes App
Viewing Os_Log Messages in Device Console
How to Update iOS 14 Widget Background Color from the App
How to Get the Opposite Value of a Bool in Swift
Swiftui Animation and Subsequent Reverse Animation to Original State