Database.database().reference() is nil - ios

I'm creating an iMessage app that needs to connect to the same database as my regular app.
I call FirebaseApp.configure() and I authenticate the user but for some reason after all of that Database.database().reference() is null even though it is supposed to be a non null value.
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
if FirebaseApp.app() == nil {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
print("FIREAPP Configed")
print(FirebaseApp.app())
signIn() //Use kept google credentials to log in
configureDatabase()
}
}
func configureDatabase() {
ref = Database.database().reference()
print(ref)
}
I'm expecting Database.database().reference() to be the root reference of the database instead of nil. Thanks for any help in advance!

I was also struggling with similar errors.
In my situation, my understanding of CocoaPods was not enough.
Before:
target 'MyApp' do
use_frameworks!
pod 'Firebase/Database'
target 'MyAppTests' do
inherit! :search_paths
end
target 'MyFrameWorkA' do
inherit! :search_paths
end
target 'MyFrameWorkB' do
inherit! :search_paths
end
end
After:
abstract_target 'AnyTargets' do # Anything that doesn't duplicate the actual target name
use_frameworks!
pod 'Firebase/Database'
target 'MyApp' do
end
target 'MyAppTests' do
end
target 'MyFrameWorkA' do
end
target 'MyFrameWorkB' do
end
end
I was able to consult your questions when looking for a solution.
I hope you solve the problem.

It seems you're confused about this line:
ref = Database.database().reference()
This merely sets up a reference to the root of the database. It doesn't load any data yet.
To actually load the data, attach a listener to the reference:
ref.observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot.value)
}) { (error) in
print(error.localizedDescription)
}
Note that data is loaded from Firebase asynchronously. Any code that needs the data, must be inside the completion handler (as above) or be called from there.
Also see the Firebase documentation on reading and writing data.

I stumbled over this error as well and the solution for me was adding 'Firebase/Core' to my pod file. I am not sure if this is common knowledge, but on setting up my firebase database project I never read about adding this specific pod.
My pod file after rewriting it:
target 'MyApp' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for MyApp
# add pods for desired Firebase products
# https://firebase.google.com/docs/ios/setup#available-pods
pod 'Firebase/Database'
pod 'Firebase/Core'
end

Related

ERROR | [iOS] file patterns: The `source_files` pattern did not match any file

I renamed my Existing library to github, I changed everything and things are working fine apart from it's not validating my library now by - pod spec lint KJCircularSlider.podspec for trunk push. I checked my folder structure and it looks perfect, anyone can help me what can be the actual issue?
Here is my library if you want to check folder structure - KJCircularSlider
Here is my podspec file.
Pod::Spec.new do |s|
s.name = 'KJCircularSlider'
s.version = '0.1.0'
s.summary = 'Circular slider - to slide from 0 to 100 in circular shape'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
It's circular slider, It provides circular shape to slide around from 0 to 100 percent, You can use it when you required a circular shape on slider rather than traditional iOS line shape slider.
DESC
s.homepage = 'https://github.com/KiranJasvanee/KJCircularSlider'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Kiran Jasvanee' => 'kiran.jasvanee#yahoo.com' }
s.source = { :git => 'https://github.com/KiranJasvanee/KJCircularSlider.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/KiranJasvanee'
s.ios.deployment_target = '9.0'
s.source_files = 'Classes/**/*'
# s.resource_bundles = {
# 'KJCircularSlider' => ['KJCircularSlider/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
I've solved my issue by changing version number of my pod.
I've renamed my KJCurveSlider library to KJCircularSlider, because of major changes in library I wouldn't be able to push it using pod trunk push. I was constantly receiving following error when I tried to validate using pod spec lint library.podspec, nevertheless I had mention perfect path of s.source_files in podspec
- ERROR | [iOS] file patterns: Thesource_filespattern did not match any file.
Then I updated version from 0.1.0 to 0.2.0, it validated successfully
I cleared the cache for my Pod and it worked!
pod cache clean YOUR_POD_NAME
the problem can occur if you have your pod source (Classes) and .podspec nested in some directory (for example /RepositoryName/Classes). So pod spec lint won't find it, regardless .podspec file is near /Classes folder.
So, optinally you can do something like this:
s.source_files = '**/Classes/**/*.{swift}'
so **/ this part does search inside nested folders.
Another option is that pod cached, you can do
pod cache clean YOUR_POD_NAME
or clean Pods cache directory
/Users/<#ur user name#>/Library/Caches/CocoaPods/Pods
P.S.
I don't think that changing the version of the pod is the right answer, because, probably all it does, force pods to use a new cache. It is not the correct answer, because you probably don't want to push a new version, but still fix the problem?
I've met this problem too. The path I set for s.source_files = 'Pod/Classes/**/*' is absolutely right. I got
The 'source_files' pattern did not match any file` error
because there's no file within classes folder. After putting my library files in it, problem solved.
For me I tried to change the Classes directory to Sources to make it future compatible with Swift Package Manager, and got this error.
- ERROR | [iOS] file patterns: The `source_files` pattern did not match any file.
the line from my podspec:
s.source_files = 'EvolvSDK/Sources/**/*'
and my file structure looked as such:
|-EvolvSDK
|-sources/
|-Utility/
|-Log.swift
I changed the sources directory name to classes and changed my podspec:
s.source_files = 'EvolvSDK/Classes/**/*'
Now it passes validation. the confusing part is that the directory is lowercase, and the podspec has uppercase. If anyone knows why that is, please add a comment as this is my first cocoapod and I'm trying to understand how it all works.
I encountered this error after creating a new pod with the pod lib create command. Wouldn't pass out of the box. For me, having the 'Classes' in the file path broke it. I changed nothing about the setup, yet it still wouldn't pass validation. I changed s.source_files = 'MyLib/Classes/*' to s.source_files = 'MyLib/**/*' and it passed. What it is, it is.
Something to keep in mind is that pod spec lint will pretty much ignore any local files other than the podspec, and try to clone/copy whatever the spec.source is set to in order to run its diagnostics.
One possible source for this error is if for some reason the spec.source is inaccessible (say, the exact tag that you specify isn't present in the repository, like a 1.0 versus v1.0 mismatch).
So making sure the repo/tag is accessible is a good debugging step if nothing else makes sense.
Also note that the result of the cloning/copying operation will be cached, stored under the pod's name/version combination, so if you make fixes without incrementing the version, you will have to clear out the cache before the fixes will work.

Charts for iOS on CocoaPods: Swift Syntax Error

The following problem appears to not occur on Xcode 8.2.1 (8C1002), but is a show-stepper on 8.3 beta (8W109m).
A project I am writing includes the Charts for iOS library via Cocoapods.
Here is my Podfile:
target 'v5snapp' do
use_frameworks!
pod 'Fabric'
pod 'Crashlytics'
pod 'Charts'
pod 'GLCalendarView', '~> 1.0.0'
pod 'MBProgressHUD', '~> 0.9.2'
pod 'RNCryptor-objc'
end
# I haven't needed this code for a while now
#post_install do |installer|
# installer.pods_project.targets.each do |target|
# target.build_configurations.each do |config|
# config.build_settings['SWIFT_VERSION'] = '3.0'
# end
# end
#end
In the Charts library, all of a sudden, there is a breaking change in the code. So, now that project won't compile, which shuts down building my whole project. Bummer.
Here is the function with the breaking code:
open override func isEqual(_ object: Any?) -> Bool {
if object == nil
{
return false
}
if !(object! as AnyObject).isKind(of: type(of: self))
{
return false
}
// This is the breaking code. The problem seems to be this:
// object! as AnyObject).data !== data
if (object! as AnyObject).data !== data && !((object! as AnyObject).data??.isEqual(self.data))!
{
return false
}
if fabs((object! as AnyObject).y - y) > DBL_EPSILON
{
return false
}
return true
}
I don't write in Swift yet, and although most of it is pretty straightforward, I'm a little fuzzy on the !, ?, and ===/!== vs. ==/!= stuff. So, what's wrong with the Swift code as-is? How can I fix it for my project? I'm tempted to just delete the offending code (leaving the other half of the line) until the library gets updated. As popular as it is, I'm sure I'm not the only one having this issue. I haven't seen this as an issue yet on the GitHub repo. I was hoping to figure out the fix myself (or here) before posting it as an issue there. Seems like just a simple problem. In the meantime, I'll go back to Xcode 8.2.1, but my devices are now on iOS 10.3 Beta, which makes it a pain to not be able to push directly to them over the wire.

XCode says 'Use of undeclared type' when trying to use a protocol defined inside a private Pod (Swift)

I and my team are stuck in a strange situation involving a base project + private pods with some protocols inside.
Our problem is we can't access some of the (protocol) identifiers (defined in a private pod) from our base app code.
Apparently our problem seems to be exactly the same as the one described in these 2 stack overflow threads, but their solutions haven't worked with us.
Thread 1: Source files not found in Swift
Thread 2: CocoaPod installed but doesn't see Swift code
The skeleton we're using is this one:
Our Commons pod (with the protocol we can't see from our base app) is defined with this .podspec file:
Pod::Spec.new do |s|
s.name = "Commons"
s.version = "0.1.10"
s.summary = "Commons framework"
s.description = <<-DESC "Commons framework"
DESC
s.homepage = "http://EXAMPLE/Commons"
s.license = { :type => "Commercial" }
s.author = { "Author" => "author#mail.mail" }
s.source = { :git => "GITLAB_PRIVATE_URL/commons-iOS-Pod.git", :tag => s.version }
s.source_files = "Commons", "Commons/**/*.{h,m,swift}"
s.exclude_files = "Commons/Exclude"
end
From our base app, we have the following Podfile (to get our Commons pod onto our main xcworkspace).
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
target 'ios-appbase' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'Alamofire'
pod 'ViewDeck'
pod 'JLRoutes'
pod 'Commons', :git => 'GITLAB_PRIVATE_URL/commons-iOS-Pod.git', :tag => '0.1.4', :branch => 'develop'
pod 'RestManager', :git => 'GITLAB_PRIVATE_URL/RestManager-iOS-Pod.git', :tag => '0.1.3'
pod 'BaseClasses', :git => 'GITLAB_PRIVATE_URL/BaseClasses-iOS-Pod.git', :tag => '0.1.3'
pod 'DesignManager', :git => 'GITLAB_PRIVATE_URL/DesignManager-iOS-Pod.git', :tag => '0.1.2'
end
We've defined our protocol as public (as it's expected to be done, because it's living inside the 'Pods' project).
What else should be looking into?
PS: Not only our custom protocol is "invisible" from our main app, but also protocols defined inside Alamofire, which is another Pod included in our main app. We try to use them and XCode complains at compile time.
Thanks in advance.
Greetings.
EDIT1: Narrowing our problems, we think it's something happening in compilation time. Why? Because XCode is able to solve the faulty symbols pressing CMD+Click (two protocols defined on our Pods file structure), but the compiler can't do it and it's the one complaining with the "Use of undeclared type" error.
Whops, success (!?)
The protocol we were defining was living inside a Framework Pod, so: What were we doing wrong?
Apparently we needed to import the name of the Framework (not the class name of the protocol (!) ). Kind of a newbie error (?).
Once we typed "import Commons" on the top part of our swift file, all "undefined xxxxxx" errors disappeared.
Not sure though if we needed this specific import part because the imported Pod was a Framework project, or we just hit the nail by chance.
I have the impression some other Classes and stuff imported from some other Pods we're using, don't need an import part, and you can directly use them from the start. So our missunderstanding might have been caused by this (?).
Anyway, and as I said before, these are newbie problems using "custom pods + swift + Frameworks + somehow complex escalable .app architectures".
Let's hope this problem and its (alleged solution) sheds some light onto future developers being in a similar situation.
Greetings.

Using Firebase push() to create a list

Here is the reference I created to my Firebase database
let ref = FIRDatabase.database().reference()
However, when I try to use this as the base_url for a push(), I get this error:
Value of type 'FIRDatabaseReference' has no member 'push'
I tried to change my base_url to this:
let ref = Firebase.database().ref()
But then I get
Module 'Firebase' has no member 'database'
Here's my Podfile. Am I missing something? I have imported Firebase at the top of my file.
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
target 'ValleybrookMessenger' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for ValleybrookMessenger
pod 'Firebase'
pod 'Firebase/Database'
pod 'Firebase/Auth'
target 'ValleybrookMessengerTests' do
inherit! :search_paths
# Pods for testing
end
target 'ValleybrookMessengerUITests' do
inherit! :search_paths
# Pods for testing
end
end
Swift doesn't have a push() method, so the equivalent you probably want is childByAutoId().
The first way you created the reference is correct:
let ref = FIRDatabase.database().reference()
Swift doesn't provide push method as javascript does, but you can perform similar action by following code
let ref = Database.database().reference("Items")
var key = ref.childByAutoId().key as? String ?? ""
ref.child(key).setValue(["Item_Name" : name])
In javascript push method automatically generates a unique key but in swift you can generate by
var key = ref.childByAutoId().key as? String ?? ""
which you can add to reference as a child and setValue inside that key.

No visible #interface in Cocoapods

I am trying to modulize my app by splitting it in different sub projects.
Inspired by this idea. So I splited my project to static libraries followed by that tutorial. I created a first library with some costume UIHelpers/Views. It has some dependencies which I defined in PodSpec file followed by AFNetworking example. (one of the dependencies is Choosy). I stored this library in bitBucket. Everything is working fine for that library(I can build it in Xcode). The problem start when I create a Model Library. I specify Git path of UIHelpers in the Model PodFile. Every time I get an error of No visible #interface for a category in Choosy (the error come from CocoaPod NOT in my project, the category is imported in the .mfile). I tried to play with link flags -ObjC,$(inhereted),-force_load included all of them.Objective-C categories in static library I cleaned the derived data.
I read the CocoaPodTroubleShoutes.
Can somebody suggest what can be tried. There is a lot of question in stuck regarding that compile error but none of them helped me.
I believe the problem somewhere in my PodSpec(cant be sure) pod lib linit I receive :- ERROR | [iOS] Choosy/Choosy/Model/ChoosyAppInfo.m:32:19: error: no visible #interface for 'UIImage' declares the selector 'applyMaskImage:completion:'
My PodSec:
Pod::Spec.new do |s|
#I tried this options:
#s.xcconfig = { 'OTHER_LDFLAGS' => $(inherited) }
#s.compiler_flags = '-ObjC'
#'-all_load'
#$(inherited)
#'-DOS_OBJECT_USE_OBJC=0', '-Wno-format'
#'-force_load'
s.prefix_header_contents = '#import <UIKit/UIKit.h>', '#import <Foundation/Foundation.h>','#import <CoreGraphics/CoreGraphics.h>','#import "ARNStyles.h"'
s.description = <<-DESC
A longer description of ARNUIHelpers in Markdown format.
DESC
s.homepage = "http://EXAMPLE/ARNUIHelpers"
s.platform = :ios, "7.0"
s.ios.deployment_target = "7.0"
s.source_files = 'UIHelpers/**/*.{h,m}'
s.requires_arc = true
s.subspec 'Choosy' do |ss|
ss.requires_arc = true
ss.compiler_flags = '-force_load'
ss.platform = :ios, "7.0"
ss.dependency 'Choosy'
# ss.xcconfig = { "FRAMEWORK_SEARCH_PATHS" => "$(PODS_ROOT)/Headers/Public/Choosy"}
# ss.ios.public_header_files = 'UIImage+ImageEffects.h'
end
s.subspec 'Dependencies' do |ss|
#ss.ios.public_header_files = 'UIImage+ImageEffects.h'
ss.requires_arc = true
ss.dependency 'FormatterKit'
end
end
In the end it was my fault. The previous developer copied those files from Choosy pod and included it manually.We still use Choosy in other places. Unfortunately the error wasn't clear enough to deduce what was wrong.

Resources