how to delete class in parse local database - parse-platform

#IBAction func unPin(sender: AnyObject) {
guestInfo.unpinInBackground()
guestInfo.deleteInBackground()
}
I add a button function to delete the guestInfo PFObject I've created.
I tried this function but it simply won't work.
I also tried delete with object names,also not working,
hoping somebody can solve this.

Related

iOS Swift 3 Deleting a row from TableView Using Parse

I am building an iOS app and I am trying to delete a row from the UITableView. I am also using Parse as the mobile-backend of the app. Here is code for the delete method:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
let query = PFQuery(className: "Receipts")
let currReceipt = receipts[indexPath.row]
query.whereKey("objectId", equalTo: currReceipt.objectId!)
query.findObjectsInBackground(block: { (objects, error) in
if error != nil {
self.createAlert(title: "Oops! Something went wrong number 1!", message: "We could not delete the receipt")
print("THERE WAS AN ERROR DELETING THE OBJECT")
}else{
for object in objects!{
self.receipts.remove(at: indexPath.row)
object.deleteInBackground()
self.table.reloadData()
}
}
})
}
}
Just to clarify, there will only be one receipt in the database with any given "objectId", so the query.findObjectsInBackground should only be returning a single object.
When I try to delete a row in the simulator, I get the error "Object not found" even though I can see that the object exists in the database. What am I doing wrong?
UPDATE
Found my solution after a long time looking. For anyone interested, it had to do with the default ACL values for Read and Write permissions. Here is the link to the answer: Parse weird bug in Swift that causes ACL write permissions to change to an objectId
It might be a typo, but why do you have ! in this line?
query.whereKey("objectId", equalTo: currReceipt.objectId!)

NSCache() is not working properly

I think I am tired of NSCache(). Could not understand what's the problem behind this. Trying to save an array of [AnyObject] to NSCahce(), which I have done using this following line of code.
NSCache().setObject(data, forKey: "News")
And tried to get it back using this way.
if let news = NSCache()("News") as? [AnyObject]
{
}
else
{
// I am always here :)
}
So I was thinking what's the problem with this. After searching a bit in Google I could see that setting totalCostLimit and countLimit will help you solve this problem. So I have set it like this.
NSCache().totalCostLimit = 50000
NSCache().countLimit = 50000
After setting this also, it was not working. So I thought of running this code in main thread, which I have done like this.
dispatch_async(dispatch_get_main_queue()) {
NSCache().setObject(data, forKey: "News")
}
Still it returned nil. Now last but not the least I have created one global instance of NSCache() and called all these operations using that instance. Well, doing like that also didn't give the expected result. It always gave me nil.
What's happening here? I know that NSCache() can store AnyObject values. I am saving lot of images in the same project without any problem, when I am trying to save this it returns nil.
Well this AnyObject contains some custom classes. Is that can be a problem? If yes, how will I save it locally without using CoreData or NSUserdefaults.
How I created an instance globally and accessed these. Created one instance of NSCache in the AppDelegate.swift file but outside of AppDelegate class
let mainCache = NSCache()
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification)
{
mainCache.totalCostLimit = 50000
mainCache.countLimit = 50000
}
}
Later I have used it like this.
mainCache.setObject(data, forKey: "News")
And getting the data back like this.
if let news = mainCache.objectForKey("News") as? [AnyObject]
{
}
else
{
// Always here :)
}
When you write NSCache() you are creating a new NSCache instance. You're doing this on just about every line.
What you need to do is create one instance, let myCache = NSCache(), and then reuse it: myCache.setObject(data, forKey: "News").

Retrieve data from new Firebase

Please help. After migrating to new Firebase I can't retrieve data.
Use this construction:
let ref = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
let postDict = snapshot.value as! [String : AnyObject]
print("\(postDict)")
})
}
After running I see error:
2016-05-19 10:04:22.264 123a[88652:13688922] The default app has not been configured yet.
2016-05-19 10:04:22.276 123a[88652:13688922] *** Terminating app due to uncaught exception 'MissingDatabaseURL', reason: 'Failed to get FIRDatabase instance: FIRApp object has no databaseURL in its FirebaseOptions object.'
*** First throw call stack:
I read documentation, but can't resolve this problem.
GoogleService-Info.plist I add to project.
I didn't see this answer yet, I had to add the configure call to the AppDelegate init method. So it looks like:
override init() {
super.init()
// Firebase Init
FIRApp.configure()
}
Had the same problem.
I looked for linking problems that are related to the plist but that wasn't the problem.
I thought maybe it has caused because of that my initial view controller is revoked before the configurations are completed.
I solved the problem by experimenting a bit.
My initial view controller was this:
let ref = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
I've changed that to this:
var ref = FIRDatabaseReference.init()
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
// Do any additional setup after loading the view, typically from a nib.
}
Crash resolved.
So, with mine, I also had a ref being declared immediately when the view controller was instantiated. I had to make it load after the app had been configured in the app delegate with FIRApp.configure().
Before:
let serverRef = Firebase("firebaseURL")
After:
lazy var serverRef = FIRDatabase.database().reference()
This delays the instantiation of the database reference until its needed, which wont be until viewDidLoad on your initial view controller.
To build on the answer given by #ColdLogic, the reason I had this error was because I had my Firebase database reference being created in an init method on a view controller, not in the viewDidLoad method. Since the init methods for all classes that are instantiated when the app launches are called before the application:DidFinishLaunchingWithOptions method in the AppDelegate, it was causing this crash. Moving this line of code:
class MyViewController {
var firebaseRef: FIRDatabaseReference
required init?(coder aDecoder: NSCoder) {
...
firebaseRef = FIRDatabase.database().reference()
}
override func viewDidLoad() {
...
}
}
to here:
class MyViewController {
var firebaseRef: FIRDatabaseReference
required init?(coder aDecoder: NSCoder) {
...
}
override func viewDidLoad() {
...
self.firebaseRef = FIRDatabase.database().reference()
}
}
solved the problem for me.
I too had a problem with the Firebase Database. Fixed it by adding
import FirebaseDatabase
to my code
Had the same problem today, you need the "firebase_url": "https://xxxxxxxxxx.firebaseio.com" at google-services.json and for that do this steps https://support.google.com/firebase/answer/7015592#android
If you had one file from google cloud platform before, maybe there are some differences and you have to check. For me this works.
In my case I had to change the configure to be called before calling the super applicationDidLaunch:
[FIRApp configure];
[super application:application didFinishLaunchingWithOptions:launchOptions];
I was getting this error until I made FIRApp.configure() the first line in the AppDelegate didFinishLaunchingWithOptions
Make sure you have downloaded the GoogleService-Info.plist file from your Firebase console and added to the root of your project directory.
Once you have added it call this function from didFinishLaunchingWithOptions in AppDelegate:
FIRApp.configure()
Thats it, it should be up and running!

'#selector' refers to a method that is not exposed to Objective-C

The new Xcode 7.3 passing the parameter via addTarget usually works for me but in this case it's throwing the error in the title. Any ideas? It throws another when I try to change it to #objc
Thank you!
cell.commentButton.addTarget(self, action: #selector(FeedViewController.didTapCommentButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
The selector it's calling
func didTapCommentButton(post: Post) {
}
In my case the function of the selector was private. Once I removed the private the error was gone. Same goes for fileprivate.
In Swift 4
You will need to add #objc to the function declaration. Until swift 4 this was implicitly inferred.
You need to use the #objc attribute on didTapCommentButton(_:) to use it with #selector.
You say you did that but you got another error. My guess is that the new error is that Post is not a type that is compatible with Objective-C. You can only expose a method to Objective-C if all of its argument types, and its return type, are compatible with Objective-C.
You could fix that by making Post a subclass of NSObject, but that's not going to matter, because the argument to didTapCommentButton(_:) will not be a Post anyway. The argument to an action function is the sender of the action, and that sender will be commentButton, which is presumably a UIButton. You should declare didTapCommentButton like this:
#objc func didTapCommentButton(sender: UIButton) {
// ...
}
You'll then face the problem of getting the Post corresponding to the tapped button. There are multiple ways to get it. Here's one.
I gather (since your code says cell.commentButton) that you're setting up a table view (or a collection view). And since your cell has a non-standard property named commentButton, I assume it's a custom UITableViewCell subclass. So let's assume your cell is a PostCell declared like this:
class PostCell: UITableViewCell {
#IBOutlet var commentButton: UIButton?
var post: Post?
// other stuff...
}
Then you can walk up the view hierarchy from the button to find the PostCell, and get the post from it:
#objc func didTapCommentButton(sender: UIButton) {
var ancestor = sender.superview
while ancestor != nil && !(ancestor! is PostCell) {
ancestor = view.superview
}
guard let cell = ancestor as? PostCell,
post = cell.post
else { return }
// Do something with post here
}
Try having the selector point to a wrapper function, which in turn calls your delegate function. That worked for me.
cell.commentButton.addTarget(self, action: #selector(wrapperForDidTapCommentButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
-
func wrapperForDidTapCommentButton(post: Post) {
FeedViewController.didTapCommentButton(post)
}
As you know selector[About] says that Objective-C runtime[About] should be used. Declarations that are marked as private or fileprivate are not exposed to the Objective-C runtime by default. That is why you have two variants:
Mark your private or fileprivate method declaration by #objc[About]
Use internal, public, open method access modifier[About]

NSCollectionView does show nothing

I've tried to follow this guide:
Quick Start for Collection Views
using an NSImageView in the Collection View Item.
Nothing shows up, neither if i set the image with a Image Well neither if i set the array via code.
So i tried to do it programmatically, using
func representedObject(representedObject: AnyObject)
{
super.representedObject = representedObject
photoImageView.image = (representedObject as! NSImage)
println("\(representedObject)")
}
in the Collection View Item (subclassed).
If I don't subclass Collection View Item Xcode tells me that there is no prototype set, if i subclass it it tells that "could not load the nibName"... (it's in the storyboard with correct identity set)
I can't have this Collection View to work :-(
Anyway, i like the bindings... so i'd like to achieve the correct result with bindings..
I checked and rechecked every passage in the document at the link and everything seems fine. the main difference is that the document uses the app delegate, i'm using a view controller.
i translated KVC methods in swift, i think they are correct since i know them have been called. Here them are:
func insertObject(p: ClientPhoto, inClientPhotoArrayAtIndex index: Int) {
images.insertObject(p, atIndex: index)
}
func removeObjectFromClientPhotoArrayAtIndex(index: Int) {
images.removeObjectAtIndex(index)
}
func setClientPhotoArray(a: NSMutableArray) {
images = a
}
func clientPhotoArray() -> NSArray {
return images
}
Their are basically 2 ways to work with NSCollectionView. 1 is to set the itemPrototype property and the other is to override newItemForRepresentedObject. The override method is more flexible and has the advantage that you using the technique below you can create the nscollectionviewitem in storyboard and all the outlets will be set correctly. Here is an example of how I use it:
class TagsCollectionView: NSCollectionView {
// ...
override func newItemForRepresentedObject(object: AnyObject!) -> NSCollectionViewItem! {
let viewItem = MainStoryboard.instantiateControllerWithIdentifier("tagCollectionViewItem") as! TagCollectionViewItem
viewItem.representedObject = object
return viewItem
}

Resources