MongoDB on Mac

1.What’s MongoDB?

MongoDB is a document database which belongs to a family of databases called NoSQL – not only SQL. In MongoDB, records are documents which behave a lot like JSON objects in JavaScript. Values in documents can be looked up by their field’s key. Documents can have some fields/keys and not others, which makes Mongo extremely flexible.

This is different than SQL databases like MySQL and PostgreSQL, where fields correspond to columns in a table and individual records correspond to rows.

2. Install and Run MongoDB with Homebrew

  • Open the Terminal app and type brew update.
  • After updating Homebrew brew install mongodb
  • The install creates:
    the configuration file (/usr/local/etc/mongod.conf)

    systemLog:
        destination: file
        path: /usr/local/var/log/mongodb/mongo.log
        logAppend: true
    storage:
        dbPath: /usr/local/var/mongodb
    net:
        bindIp: 127.0.0.1
    

    the log directory path (/usr/local/var/log/mongodb)
    the data directory path (/usr/local/var/mongodb)

3. Run the MongoDB daemon

In one of your terminal windows run mongod. This should start the Mongo server.
Oops, something is wrong:

[main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
[initandlisten] MongoDB starting : pid=13410 port=27017 dbpath=/data/db 64-bit host=Zhihuis-Mac-mini.local
[initandlisten] db version v4.0.3
[initandlisten] git version: 7ea530946fa7880364d88c8d8b6026bbc9ffa48c
[initandlisten] allocator: system
[initandlisten] modules: none
[initandlisten] build environment:
[initandlisten]     distarch: x86_64
[initandlisten]     target_arch: x86_64
[initandlisten] options: {}
[initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
[initandlisten] shutdown: going to close listening sockets...
[initandlisten] removing socket file: /tmp/mongodb-27017.sock
[initandlisten] now exiting
[initandlisten] shutting down with code:100

It complains that exception in initAndListen: NonExistentPath: Data directory /data/db not found.

You can either

  • create the db folder manually by mkdir -p /data/db, Make sure that the /data/db directory has the right permissions by running

    > sudo chown -R `id -un` /data/db
    > # Enter your password
    

    or

  • Specify the mongod.conf when Running the daemon mongod --config /usr/local/etc/mongod.conf. The db file locates in /usr/local/var/log/mongodb

Alternatively, to run MongoDB as a macOS service, issue the following (the process uses the /usr/local/etc/mongod.conf file, created during the install):
brew services start mongodb

4. Connect and Use MongoDB

mongo

5. Exit/Stop the Mongo

To exit the Mongo shell run quit()
To stop the Mongo daemon hit ctrl-c

Reference

Install MongoDB Community Edition on macOS

Posted in Mobile | Tagged | Leave a comment

Python3 Virtualenv Setup

Install python3

brew install python3

Pip3 is installed with Python3

Upgrade virtualenv

To install virtualenv via pip run:

pip install --upgrade virtualenv

Create virtualenv

virtualenv -p python3 python3-venv

Activate the virtualenv

source python3-venv/bin/activate

Deactivate the virtualenv

deactivate
Posted in Mobile | Tagged | Leave a comment

LLDB you should know

LLDB is a next generation, high-performance debugger. It is built as a set of reusable components which highly leverage existing libraries in the larger LLVM Project, such as the Clang expression parser and LLVM disassembler.
LLDB is the default debugger in Xcode on Mac OS X and supports debugging C, Objective-C and C++ on the desktop and iOS devices and simulator.

LLDB syntax:

<command> [<subcommand> [<subcommand>...]] <action> [-options [option-value]] [argument [argument...]]

1. breakpoint

set breakpoints for all some:

breakpoint set -r some

-r: All method names including some will be set breakpoint
-n: All method names with some will be set breakpoint

// set breakpoint for all methods which including `viewDidLo`:
breakpoint set -r viewDidLo

// set breakpoint for all methods `viewDidLoad`:
breakpoint set -n viewDidLoad

// list all breakpoints
breakpoint list

// disable/enable all breakpoints
breakpoint disable/enable

// delete all breakpoints
breakpoint delete

2. register

register read Show current thread General Purpose Register:

(lldb) register read
General Purpose Registers:
       rax = 0x0000000000000001
       rbx = 0x0000000110d16c05  "count"
       rcx = 0x00007ff78b873f28
       rdx = 0x0000000000000000
       rdi = 0x00007ff78a832400
       rsi = 0x0000000120e1cc47  "viewDidLoad"
       rbp = 0x00007ffee43f7150
       rsp = 0x00007ffee43f7150
        r8 = 0x00000000000001ff
        r9 = 0x00006000029266c0
       r10 = 0x0000000000000047
       r11 = 0x000000010b90ff90  Facebook`@objc Facebook.BrowseViewController.viewDidLoad() -> () at <compiler-generated>
       r12 = 0x0000000000000018
       r13 = 0x00007ff78a832400
       r14 = 0x000000012156b9e4  UIKitCore`_UIApplicationLinkedOnVersion
       r15 = 0x0000000110206d80  libobjc.A.dylib`objc_msgSend
       rip = 0x000000010b90ff94  Casino`@objc Facebook.BrowseViewController.viewDidLoad() -> () + 4 at <compiler-generated>
    rflags = 0x0000000000000246
        cs = 0x000000000000002b
        fs = 0x0000000000000000
        gs = 0x0000000000000000

Write a new decimal 124 to current thread rax

register write rax 123

Posted in iOS | Tagged , , | Leave a comment

Xcode instruments

The two columns worth noting in Instruments are # Persistent and # Transient. The Persistent column keeps a count of the number of objects of each type that currently exist in memory. The Transient column shows the number of objects that have existed but have since been deallocated. Persistent objects are using up memory, transient objects have had their memory released.

Posted in iOS, Mobile | Tagged | Leave a comment

Git tips

How do I discard changes in my working copy that are not in the index?

git stash save --keep-index --include-untracked

You don't need to include –include-untracked if you don't want to be thorough about it.

or drop the stash

git stash drop

or all unstaged files in current working directory use:

git checkout -- .

For a specific file use:

git checkout -- path/to/file/to/revert

Add alias:

git branch -a| grep xxx

# ~/.gitconfig
[alias]
        squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
        last = log -1 HEAD
        bag = "!f() { git branch -a | grep ${1} --color=auto; }; f"

Reference

Posted in git | Tagged | Leave a comment

Views vs. layers

A layer is a simple model class that exposes a number of properties to represents some image-based content. Every UIView is backed by a layer, so you can think of layers as the lower-level behind the scenes class behind your content.
A layer is different from a view (with respect to animations) for the following reasons:
A layer is a model object – it exposes data properties and implements no logic. It has no complex Auto Layout dependencies nor does it handle user interactions.
It has pre-defined visible traits – these traits are a number of data properties that affect how the contents is rendered on screen, such as border line, border color, position and shadow.
Finally, Core Animation optimizes the caching of layer contents and fast drawing directly on the GPU.
To compare views and layers side by side:

Views

  • Complex view hierarchy layouts, Auto Layout, etc.
  • User interactions.
  • Often have custom logic or custom drawing code that executes on the main thread on the CPU.
  • Very flexible, powerful, lots of classes to subclass.

Layers

  • Simpler hierarchy, faster to resolve layout, faster to draw.
  • No responder chain overhead.
  • No custom logic by default. and drawn directly on the GPU.
  • Not as flexible, fewer classes to subclass.

If you need to choose between views and layers here is tip: choose view animations any time you can to do the job; you will know when you need more performance or flexibility and have to switch to layer animations instead.

Posted in iOS, Mobile | Tagged | Leave a comment

Xcode tips and ticks

Faster Testing

When using TDD you can work more quickly by running a subset of tests – press ⌃⌥⌘G to re-run only your last test, or ⌘-click several tests to run only them.

Opening Xcode

If you’re in a folder that has both a workspace and a project, use xed . to open the workspace in Xcode. If there were only a project, that would have been opened instead.

Generated interfaces

If you're looking at a Swift data type and just want a summary of what it does, press ⌃⌘↑ to have Xcode generate an interface showing only its external properties and methods.

Filtering the jump bar

If you find Xcode's jump bar a little long, try filtering it. After pressing ⌃6 to bring up the jump bar, you can start typing a few letters to filter all your properties and methods using a fuzzy search.

Clearing up space

If you’ve been using Xcode for a few years, you should run xcrun simctl delete unavailable to remove any old simulators that are no longer supported. It's common to save 20-60GB with that one command!

Watchpoints

When your program is paused, right-click on a variable and choose Watch. The next time that variable changes, you can step back one frame to see where it happened.

Identifying constraints

It’s common to see lots of output in Xcode’s log when you have an Auto Layout problem, but if you add identifiers to your constraint you’ll get more helpful messages – easy to do in both Interface Builder and code!

Measuring build time

If you've ever wondered how long it takes for Xcode to build your project, you can find out with one Terminal command.

defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES

Show build time

defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
Posted in iOS, Mobile | Tagged | Leave a comment