How to programmatically check whether the current frontend visitor is logged in

Method 1 (obvious, but does not work in some situations)

It is similar to Magento 1.x

/** @return bool */
function rm_customer_logged_in() {
	/** @var \Magento\Framework\ObjectManagerInterface $om */
	$om = \Magento\Framework\App\ObjectManager::getInstance();
	/** @var \Magento\Customer\Model\Session $session */
	$session =  $om->get('Magento\Customer\Model\Session');
	return $session->isLoggedIn();
}

Method 2 (the recommended way)

/** @return bool */
function rm_customer_logged_in_2() {
	/** @var \Magento\Framework\ObjectManagerInterface $om */
	$om = \Magento\Framework\App\ObjectManager::getInstance();
	/** @var \Magento\Framework\App\Http\Context $context */
	$context =  $om->get('Magento\Framework\App\Http\Context');
	return $context->getValue(Magento\Customer\Model\Context::CONTEXT_AUTH);
}